11 Replies - 1013 Views - Last Post: 10 April 2017 - 03:31 PM Rate Topic: -----

#1 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Question about code going in sequence

Posted 07 April 2017 - 07:17 PM

Hey guys sorry if this seems like a noob question but if I can get this answered I will be free and will have stopped wasting time figuring out this crap.

So basically in a script (also will be using Unity3d) it goes from top to bottom and it can only execute 1 thing at a time.

My question is say for example we have a input(getKeyDown(Keycode.F)) {debug.log("you pressed a button" };

Pretend that code is on line 5, and we have the next code debug.log statement for example on line 6. If the user was to press a key it can't jump from line 6 to line 5 right? Like it has to go all the way down then back up?

I ask because Basically I get a certain exception and I know why its trying to find a transform but doesn't exist. Is it because the code is been executed really fast and it doesn't register my key because I havent pressed and so it goes to the next line and I get an exception?

This is the code in short:

        if (Input.GetKeyDown(KeyCode.LeftControl) && Guns.Count != 0 && weaponFireType != "Shotgun")
        {
            //make a new gameObject which will hold the bullet in the scene it will instantiate a bullet at the position of the bulletPosition empty gameObject.
            bulletInScene = (GameObject)Instantiate(BulletPrefab, bulletPosition.transform.position, Quaternion.identity);

            //the transform of the player
            cameraTransform = player.transform;
            //Makes the player the parent of the GameObject currentGun
            bulletInScene.transform.SetParent(cameraTransform, false);
            Debug.Log("BULLET: " + bulletInScene.transform.parent);
        }




Then error is here

        foreach (Transform t in transform)
        { 
            if (bulletInScene != null)
            {
                if (t.name.Contains("Bullet1(Clone)"))
                {
                    //error here because bullet1 doesn't exist
                    transform.Find("Bullet1(Clone)").transform.parent = null;
                }
            }
        }



I am almost 100% certain but want to confirm I will not get the exception while the bullet is under the transform it will only get the error while there is no bullet under the transform. So since the user hasn't pressed a key it goes to the next line until it reaches the "transform.find" line then I get the exception is this correct?

Is This A Good Question/Topic? 0
  • +

Replies To: Question about code going in sequence

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: Question about code going in sequence

Posted 07 April 2017 - 08:33 PM

Why are you using String.Contains(). You do realize that does a substring match. So if you search for "dad", you will find that the words "dad", "doodad", and "daddy" will all satisfy the condition. I assume that the tranform.Find() performs an exact string match rather than a substring match.
Was This Post Helpful? 0
  • +
  • -

#3 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Question about code going in sequence

Posted 07 April 2017 - 10:18 PM

Don't worry about the code, I have a much better improved version I just wanted to confirm something. Is my description correct?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: Question about code going in sequence

Posted 08 April 2017 - 05:08 AM

My point about the use of Contains() is that the if maybe true because there is a transform that has that sub string you are searching for, but the Find() can't find an exact string and therefore it is returning a null result.

As another aside, if you already know that the name of the transform t satisfies the condition, why are you searching for it again? You already have a reference to it as t.

Yes, code should run sequentially. Are you sure that those two different chunks of code are running within the same thread? Or is the first chunk run in the UI thread, while the second chunk running in a thread pool. In that case, the two chunks of code could run in parallel. You'll need to use some critical sections to protect the resource that is shared between the threads.
Was This Post Helpful? 0
  • +
  • -

#5 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Question about code going in sequence

Posted 08 April 2017 - 09:07 AM

You are going off topic again, I told you I have better code I was just curious. I have no idea about threads, anyways I think my theory was correct that the reason the exception is popping up is because it checks to see if key was pressed wasnt so will go to next line of code until eventually the transform.find and error.

This post has been edited by Skydiver: 08 April 2017 - 12:14 PM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? -1
  • +
  • -

#6 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: Question about code going in sequence

Posted 08 April 2017 - 01:09 PM

It is not off topic. It is very relevant to your question about whether code will run in sequence.

To answer your question directly about if statements and sequence of execution, given code:
if (condition)
    executeIfConditionIsTrue();
executeAfterIf();



The execution order will proceed to line and and determine if condition is true. If it is true, then executeIfConditionIsTrue() will be executed next, then followed by executeAfterIf(). If it is false, the executeAfterIf() will be executed.

So why is my harping about your string comparisons relevant to your question? Let's review your code:
foreach (Transform t in transform)
{ 
    if (bulletInScene != null)
    {
        if (t.name.Contains("Bullet1(Clone)"))
        {
            //error here because bullet1 doesn't exist
            transform.Find("Bullet1(Clone)").transform.parent = null;
        }
    }
}



To enter the block within lines 3-10, transform must to be null, and the collection must not be empty. To enter the block within lines 4-9, bulletInScene must not be null. To enter the block within lines 7-8, t.name must contain the substring ""Bullet1(Clone)"". For the crash on line 8 to happen, either:
  • transform is null;
  • the return value of Find() is null; or
  • the transform member of the Transform returned returned Find() is null.


We know that transform is not null, otherwise we wouldn't have even entered the block of lines 3-10.

There is a possibility of the return value of Find() being null because the Unity Transform.Find() documentation tells us:

Quote

If no child with name can be found, null is returned.


But how is it possible that Find() will fail, but we got into the block lines 7-8? This is possible because you used String.Contains() which matches substrings. Presumably, Transform.Find() matches exact substrings. So it is possible for the condition on line 5 to be true, but for Find() to fail.

Okay, but what about the third option where Find() actually succeeds and returns a Transform that matches? Is it possible for the transform member of that Transform to be null? I don't really know since I don't use Unity, but if it were null, then your other chunk of code would also be crashing.

For reference, you had this in your other chunk of code.
if (Input.GetKeyDown(KeyCode.LeftControl) && Guns.Count != 0 && weaponFireType != "Shotgun")
{
    //make a new gameObject which will hold the bullet in the scene it will instantiate a bullet at the position of the bulletPosition empty gameObject.
    bulletInScene = (GameObject)Instantiate(BulletPrefab, bulletPosition.transform.position, Quaternion.identity);

    //the transform of the player
    cameraTransform = player.transform;
    //Makes the player the parent of the GameObject currentGun
    bulletInScene.transform.SetParent(cameraTransform, false);
    Debug.Log("BULLET: " + bulletInScene.transform.parent);
}


Lines 9 and 10 reference bulletInScene.transform. If bulletInScene.transform were null, this code would crash on line 9, even before you get to your for each where you are searching for the clone of the prefab.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: Question about code going in sequence

Posted 08 April 2017 - 01:31 PM

So the question is, how is bulletInScene not null within the foreach block? It looks like the variable set to an instance within the success block of your keypress check based on the code that you presented. When is set to null?

If the variable is local to a method shared by both blocks of code that you presented, then if the key was not pressed, then the variable should not be null and you won't get into that crashing situation.
Spoiler


On the other hand, if the variable is some kind of class variable, when does it ever get set back to null if the key is not pressed? In this case, it is quite possible to satisfy the first condition within the foreach block.
Spoiler

Was This Post Helpful? 0
  • +
  • -

#8 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Question about code going in sequence

Posted 08 April 2017 - 05:32 PM

Ah ok cheers. When I said off topic I was referring to when you were saying for example "why are you using the contains" doesn't matter. But just help me answer 1 thing, code goes from top to bottom if the user didnt press the key it will ignore that statement and eventually we will go in the loop. I tested with a debug.log I think its right I put a debug.log inside the if statement and before the error at transform and the debug.log before the transform keeps going and going so makes sense.
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: Question about code going in sequence

Posted 09 April 2017 - 05:39 PM

View PostDialupp, on 08 April 2017 - 08:32 PM, said:

When I said off topic I was referring to when you were saying for example "why are you using the contains" doesn't matter.

It is not off topic. If the issue looks to be due to the substring search succeeding, but the exact string failing, clearly you must have been interested in the transform that satisfied the substring match. Since you already had a reference to it as t, why search for it again? That would fix the exception.

View PostDialupp, on 08 April 2017 - 08:32 PM, said:

But just help me answer 1 thing, code goes from top to bottom if the user didnt press the key it will ignore that statement and eventually we will go in the loop. I tested with a debug.log I think its right I put a debug.log inside the if statement and before the error at transform and the debug.log before the transform keeps going and going so makes sense.


What is the question?
Was This Post Helpful? 0
  • +
  • -

#10 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Question about code going in sequence

Posted 10 April 2017 - 02:34 AM

Oh yeah now that you said it like that you're right my apologies. Oh yeah I didn't even ask a question lol. I think I didn't mean a question it was more to see if my description was correct. But I'll try put it in a question. Is it true while the error is playing (its just keeps on going because there is no bullet1(clone)) that for that split second or so that when the user presses the key and the bullet is attatched to the player that the exception doesn't pop up for that small nano second or so we just cant see it? But the error does pop up when there is no bullet?

This post has been edited by Skydiver: 10 April 2017 - 04:27 AM
Reason for edit:: Removed unnecessary quote. No need to quote the post above yours.

Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 5890
  • View blog
  • Posts: 20,101
  • Joined: 05-May 12

Re: Question about code going in sequence

Posted 10 April 2017 - 05:24 AM

Assuming that everything is running on a single thread within a single method and that bulletInScene is initialized to null:
No, the error popup up means that the user already pressed the button because that is the only way to get into that line throws the exception based on the code that you have shown.

Yes, the error does pop up when there is no bullet that matches exactly.

On the other hand, if those two chunks of code are running concurrently and/or bulletInScene is not correctly to null when here is no bullet, and to null when there is a bullet, then there are other failure modes and you'll get the exception.
Was This Post Helpful? 0
  • +
  • -

#12 Dialupp  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 210
  • Joined: 31-October 16

Re: Question about code going in sequence

Posted 10 April 2017 - 03:31 PM

Ah ok thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1