C# : The name `' does not exist in the current context unity3d

Discussion in 'Scripting' started by itanium, Sep 8, 2012.

  1. itanium

    itanium

    Member

    Joined:
    Feb 23, 2012
    Messages:
    11
    Hello,
    I've searched a lot using google and forum searcher, but I didn't find answer for my problem.

    My problem :
    Why does this
    Code (csharp):
    1.    
    2. void Sart ()
    3.     {
    4.         GameObject monster = GameObject.Find ("monster");
    5.     }
    6. void Update ()
    7.         {
    8.     CharacterController controller = monster.GetComponent<CharacterController>();
    9.         }
    10.  
    or this code
    Code (csharp):
    1.    
    2. void Sart ()
    3.     {
    4.         CharacterController controller = GameObject.Find ("monster").GetComponent<CharacterController>();
    5.     }
    6.         void Update ()
    7.         {
    8.         guiText.text = "Speed : " + controller.velocity.y.ToString();
    9.     }
    10.  
    gives me a context related error? For the first code he can't find monster and for the second : controller.
    Why? I tried also using Awake() instead of Start(), but keep getting the same error.
     
  2. Brian Stone

    Brian Stone

    Member

    Joined:
    Jun 9, 2012
    Messages:
    221
    Because it's telling you the truth. Those variables are not in the context that they're being called. The variable "monster" is defined inside the Start() method, so it can only be used inside the Start() method. Once Start() returns, the "monster" variable is thrown away, its memory is deleted. If you want "monster" to be available in the Update() method, then you need to define the variable inside the class context. In other words, you need to make it global, like this...

    Code (csharp):
    1.  
    2. class MyClass : MonoBehavior
    3. {
    4.    GameObject monster;
    5.  
    6.    void Start ()
    7.    {
    8.       monster = GameObject.Find ("monster");
    9.    }
    10.  
    11.    void Update ()
    12.    {
    13.       CharacterController controller = monster.GetComponent<CharacterController>();
    14.    }
    15. }
    16.  
     
    Last edited: Sep 9, 2012
  3. itanium

    itanium

    Member

    Joined:
    Feb 23, 2012
    Messages:
    11
    Thank you very much for your answer.
    It solved my problem and there's no need for a monster initialization in start(), just drag object in inspector to the script and everything is fine.
    Without dragging there was a error
     
  4. Brian Stone

    Brian Stone

    Member

    Joined:
    Jun 9, 2012
    Messages:
    221
    Sorry for missing that. That error pops up because the compiler doesn't know how Unity's engine works, or how any program works, for that matter. It doesn't know that Start() is guaranteed to be called before the Update() method, but it sees the possibility that Update() is called before "monster" is initialized.

    To avoid that error, you simply need to initialize the variable to something. In this case, you should set the variable to "null", because GameObject is a reference type. Alternatively, because this class inherits from MonoBehavior and you've attached it to a GameObject in the Unity editor, you can set the variable in the inspector. Unity generates the initialization code for the variable automatically prior to sending the program to Mono to be compiled.

    Code (csharp):
    1.  
    2.    GameObject monster = null;
    3.    .
    4.    .
    5.    .
    6.  
     
  5. kingcharizard

    kingcharizard

    New Member

    Joined:
    Jun 30, 2011
    Messages:
    1,134
    your code
    Code (csharp):
    1. void Sart ()
    what I think you want

    Code (csharp):
    1. void Start()
     
  6. Brian Stone

    Brian Stone

    Member

    Joined:
    Jun 9, 2012
    Messages:
    221
    Minor spelling error. I've corrected it in my post. OP may or may not have copy/pasted that code directly from his project. In either case, it doesn't change the status of the original problem.