Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Evaluating an unset vector [Resolved]

Discussion in 'Scripting' started by Marscaleb, Jun 13, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    This is a bit of code from my projectile class's update function:
    Code (CSharp):
    1. if (lastLocation == null) {
    2.             lastLocation = transform.position;
    3.             return;
    4.         }
    (lastLocation is a vector3.)
    The idea is that I want it to skip certain code on the first update. Then on each subsequent update it linecasts between the spot it was in the last frame and this frame, runs some magic, and then updates the lastLocation.

    But I get this compile warning that says "The result of comparing value type `Projectile.lastLocation' with null is `false'"
    Well, okay then, a Vector3 can't be null.
    How then do I detect if this variable has been set or not? How can I tell if this is the very first time update has run?
    If I try to run the code without checking that, then I will get a linecast from the objects current position to the world origin.
    I suppose I could compare it to a vector of 0,0,0, but then I run into the extremely unlikely scenario where if a projectile happens to move exactly through the world origin it will skip a whole update cycle.

    The only solution I can think of is to create a new bool variable just to check if this is the first update or not. Granted, it's not going to excessively increase my memory costs or anything, but it seems like there should be a more efficient way to do this. Is there some better way to check if a vector3 has been set or not?
     
  2. one_one

    one_one

    Joined:
    May 20, 2013
    Posts:
    621
    You're assigning a new Vector3 to lastLocation right after declaring it, right? Try not to do that. That should let you do what you want to.
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    No, it's declared at the top of the script. In the update function, I am checking it against null, but I'm being told by the editor that it will always evaluate to false. So I can't evaluate it against null because that would be pointless; it's not returning what I thought it would.
     
  4. one_one

    one_one

    Joined:
    May 20, 2013
    Posts:
    621
    Right, right. If you think skipping a single update frame for a very unlikely scenario is worth it, you can either go with your suggestion or make your lastPosition Vector3 nullable. But it doesn't seem like either of that would be really worth it and just comparing it to (0,0,0) seems like the best option.
     
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    if i get it right you just want check if the object has move if not skip your update?
    if so just check your current position to previous position if they are the same return else continue...?
     
  6. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    977
    That's pretty clever. The only problem is that when it is first initialized it will register that vector3 as (0,0,0) and not its current position. Although simply forcing it to update that in the start function would solve that problem.

    Although, that runs into the potential problem of the projectile not working if it never moved that frame, like if it had some unique path or pattern where it stopped or something, and then another actor moved into the projectile... Altogether, slightly more inconceivable than it passing through the world origin at the exact moment it reaches the end of a frame.
    ...And all this to avoid adding a single bool to the memory for an object destined to disappear in a few moments.

    You know, coming back to this problem a week later has helped me to realize how stupid this whole problem is.

    Also, I totally just added a line to set the lastPostition to the current position in the Start function and completely solved the whole problem. Now I no longer need to skip that first update because it won't be drawing a linecast between the world origin and its first position.