Search Unity

Variable has 2 values.....

Discussion in 'Scripting' started by Deleted User, Dec 2, 2016.

  1. Deleted User

    Deleted User

    Guest

    Alright, I have been breaking my head on this one:

    Assigning + Debug.Log code:
    Code (CSharp):
    1.     public Transform[] gfsdg6775h5s1fdgf4ds6g567fsdg65sdfg78dfsg56sfd6fg8dfsgdfsgfd36g6f45d;
    + Debug.Log code:
    Code (CSharp):
    1. void Update()
    2.     {
    3.         Patrolling();
    4.     }
    5.  
    6.     public void Patrolling()
    7.     {
    8.         Debug.Log("Waypoint count: " + gfsdg6775h5s1fdgf4ds6g567fsdg65sdfg78dfsg56sfd6fg8dfsgdfsgfd36g6f45d.Length);
    9. }
    (Dont minf the variable name, its a bad name but i got kinda irritated, so tried with superrandom name.


    Anyways, The Gameobject:



    So, all normal right? You would think so!

    Expect, the Debug Log:



    It doesnt throw any errors, just the values 0 and 5. I have been breaking my head over it, and its happening nowhere else then in this script. (Yes i checked, nothing else is using this script)
     
  2. MrArcher

    MrArcher

    Joined:
    Feb 27, 2014
    Posts:
    106
    The images you posted didn't work, but I found them by inspecting the source.

    Your Debug.Log is getting called from another instance of the class. There must be a second one in your scene. Easiest way to find it would be to search in the Hierarchy by type.
     
    Deleted User likes this.
  3. takatok

    takatok

    Joined:
    Aug 18, 2016
    Posts:
    1,496
    If you ever get a wonky error like this is usually from having more than one instance of the script attached to different gameObjects. Try adding this to your Debug.Log to confirm its the same GO. You'll often see it isn't:
    Code (CSharp):
    1. Debug.Log("GameObject: " + GetInstanceID() + "Rest of your Debug");
    Then you can track down where you added the script an extra time and forgot about it :)
     
    Deleted User and Xepherys like this.
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Extra points for giving the variable a bad name just to show it who's boss though. :D
     
    Deleted User and KelsoMRK like this.
  5. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
    Even better - use the overload of Log that takes a GameObject and it will highlight it in the scene when you select the log statement in the console
    Code (csharp):
    1.  
    2. Debug.Log("instance", gameObject);
    3.  
     
    Deleted User, lordofduct and Stardog like this.
  6. Deleted User

    Deleted User

    Guest

    Sorry for the late answer, @KelsoMRK and @takatok, both of these did the job, thanks! Apperantly it was sitting on the terrain (Don't ask my why), Thanks for the help! :D