Search Unity

Variables keep clearing unless set to "static"

Discussion in 'Getting Started' started by Tset_Tsyung, Feb 3, 2016.

  1. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    [Solved - sorta]

    Okay after removing a couple of layers from the 'communication' between the planet being clicked on the ship moving it seems to have cleared up the problem. I'm still not sure WHY it happened in the first place, OR why changing the variables to 'static' worked... but they did. Sorry for taking up anyone's time. (Moderators, feel free to delete/bury/whatever this thread as you see fit)

    Hey all,

    I've already found a solution for this problem, but wanting to understand WHY it's happening...

    Here's my code:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PlayerMovement : MonoBehaviour {
    5.  
    6.     public float m_movementSpeed = 50f;
    7.  
    8.     public GameObject destinationPlanet;
    9.     public bool destinationAssigned;
    10.  
    11.     // Use this for initialization
    12.     void Start ()
    13.     {
    14.         //destinationAssigned = false;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update ()
    19.     {
    20.         Debug.Log ("destinationAssigned is " + destinationAssigned + " at time " + Time.time);
    21.         // if we have a destination target travel towards it
    22.         if (destinationAssigned)
    23.         {
    24.             Debug.Log ("Moving to destination");
    25.             transform.position = Vector3.MoveTowards (transform.position, destinationPlanet.transform.position, m_movementSpeed * Time.time);
    26.         }
    27.  
    28.     }
    29.  
    30.     public void SetNav(GameObject targetPlanet)
    31.     {
    32.         destinationPlanet = targetPlanet;
    33.         destinationAssigned = true;
    34.         Debug.Log ("Destination system is set to " + destinationPlanet.transform.name + " and destinationAssigned is " + destinationAssigned + " at time " + Time.time);
    35.     }
    36. }
    Now, when I click on a planet, the planet contacts the game controller, which in turn contacts the player ships and hands it the gameobject of the planet (need to follow the planet as it moves, hence handing it the GameObject and not just the transform).

    Now the problem is that I SEE that this script is receiving the planet's object AND it's setting the boolean 'destinationAssigned' to true. But at the same time frame, when it hits Update(), both the boolean and the GameObject "destinationPlanet" are reset to NULL.

    Why is it doing this?

    My small workaround is to set destinationPlane & Assigned to 'public static' - and since there's only 1 player ship at this stage, that's fine. And this works! But I've never had this problem before, any ideas as to why it's doing this?

    Many thanks in advance.

    Mike
     
    Last edited: Feb 3, 2016
  2. Farelle

    Farelle

    Joined:
    Feb 20, 2015
    Posts:
    504
    where is the void SetNav being called? I'm not seeing that this method is ever being used
     
  3. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    My guess is that you have more than one instance of this script in your scene, without realizing it.
     
  4. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    @Farelle It's a public call, so it was getting called originally by the GameController, and now just by the planet directly

    @JoeStrout Yeah, that was my thought as well... but there was only one in inspector (that I could tell) during run time.

    As I mentioned on the EDIT at the top, I removed the need for the GameController to get involved at all by having the planet clicked on find the player object with FindWithTag(). Then I'm just sending the gameobject through directly... for SOME reason this seems to have corrected the issue. Not sure why, but I guess it's something I'll have to bear in mind - not involving what doesn't need to be involved = better...
     
  5. jhocking

    jhocking

    Joined:
    Nov 21, 2009
    Posts:
    814
    Post the GameController script. Without seeing that, we have no idea what that script was doing wrong. While unnecessary indirection can sometimes be undesirable for readability reasons, it shouldn't cause variables in other scripts to randomly change value.
     
  6. Farelle

    Farelle

    Joined:
    Feb 20, 2015
    Posts:
    504
    and it does sound like it has to do with the game controller script :rolleyes:
     
  7. Tset_Tsyung

    Tset_Tsyung

    Joined:
    Jan 12, 2016
    Posts:
    411
    @jhocking and @Farelle,

    Sorry, forgot to check the messages, lol. Are you still interested in looking at the script? Sorry, been working on another project (waiting for a friend to get on board with this one - although I'm not sure he ever will, lol)