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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

The variable is assigned but its... [solved itself]

Discussion in 'Scripting' started by TwiiK, Mar 28, 2010.

  1. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    My log is full of these. They are warnings, but they are annoying and I'm not sure how I can get rid of them. Not even sure why they appear in the first place.

    This code for instance generates a warning:

    Code (csharp):
    1. // Create a rocket
    2. Rigidbody instantiatedProjectile = Instantiate (rocket, transform.position, transform.rotation) as Rigidbody;
    3.  
    4. // Ignore collisions between the rocket and the rocket launcher
    5. Physics.IgnoreCollision(instantiatedProjectile.collider, transform.root.collider);
    6.  
    I am using the value of instantiatedProjectile on the second line am I not? How else would I tell the rocket to ignore collisions without first declaring it a variable?

    And if it's a bug that these warnings appear is there a trick to make them not appear? Like doing something arbitrary with the variable to make the compiler think it's been used?
     
  2. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,729
    Actually, scratch that.

    Not sure what I did, but suddenly cases like the above code didn't generate warnings anymore so I guess it fixed itself. :)
     
  3. fallingbrickwork

    fallingbrickwork

    Joined:
    Mar 16, 2009
    Posts:
    1,072
    Sorry but I couldn't help myself, I saw the title '[solved itself]', and had to have a look at the thread... I only wished my problems 'solved themselves'.

    Glad you got your issue sorted enough to proceed, and if not, well you made me smile.

    Best regards,
    Matt.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Still, you should probably say what the warning actually is. Or was.

    --Eric
     
  5. n0mad

    n0mad

    Joined:
    Jan 27, 2009
    Posts:
    3,732
    old topic, but I got the same warnings since U3 (even beta).

    Any idea about where does it come from ?

    example of warning :

    But I'm really using those variables in the very same class they are declared ... :confused:

    What's even more odd is that they were set on a private state formerly, but I deleted that state, and the warning is still hinting me as a private variable ... what the ? :eek:

    P.S : yes it's really annoying :/

    edit : whenever I type these variables as public, the warning is gone :confused:
     
    Last edited: Sep 28, 2010
  6. ofaaron

    ofaaron

    Joined:
    May 23, 2010
    Posts:
    52
    I'm getting the same problem as TwiiK but the problem has not resolved itself. The code seems to be working as intended though, but these errors are very irritating.

    The code in question is here:

    Code (csharp):
    1.  
    2. //the declaration
    3. public Transform bot;
    Code (csharp):
    1.  
    2. //where it is used
    3. botObj = (Instantiate(bot, transform.position, Quaternion.identity) as Transform).gameObject;
    Again, the variable IS assigned in the editor, and the code is working as intended. I just would like to get these errors to go away.
     
  7. geetoo

    geetoo

    Joined:
    Apr 21, 2009
    Posts:
    42
    What if you set your bot variable to be a GameObject and not a Transform ? Does it change anything ?
     
  8. ofaaron

    ofaaron

    Joined:
    May 23, 2010
    Posts:
    52
    I get the same error, and also lots of other problems appear with the code because of how that variable is used in other classes.
     
  9. ofaaron

    ofaaron

    Joined:
    May 23, 2010
    Posts:
    52
    The problem has been resolved.

    It was caused by my own carelessness. I thought the error was being caused by the script attached to the object to which it is supposed to be attached (as one would.) However, I had accidentally attached a copy of the script to a random environment object, which is what was calling the error.
     
    Last edited: Feb 21, 2011