Unity Community


Results 1 to 6 of 6

  1. Posts
    9

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

    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:  
    1. void Sart ()
    2.     {
    3.         GameObject monster = GameObject.Find ("monster");
    4.     }
    5. void Update ()
    6.         {
    7.     CharacterController controller = monster.GetComponent<CharacterController>();
    8.         }
    or this code
    Code:  
    1. void Sart ()
    2.     {
    3.         CharacterController controller = GameObject.Find ("monster").GetComponent<CharacterController>();
    4.     }
    5.         void Update ()
    6.         {
    7.         guiText.text = "Speed : " + controller.velocity.y.ToString();
    8.     }
    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. Location
    Sammamish, WA
    Posts
    220
    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:  
    1. class MyClass : MonoBehavior
    2. {
    3.    GameObject monster;
    4.  
    5.    void Start ()
    6.    {
    7.       monster = GameObject.Find ("monster");
    8.    }
    9.  
    10.    void Update ()
    11.    {
    12.       CharacterController controller = monster.GetComponent<CharacterController>();
    13.    }
    14. }
    Last edited by Brian Stone; 09-08-2012 at 04:05 PM.


  3. Posts
    9
    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
    The variable monster of 'speed' has not been assigned.


  4. Location
    Sammamish, WA
    Posts
    220
    The variable monster of 'speed' has not been assigned.
    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:  
    1.   GameObject monster = null;
    2.    .
    3.    .
    4.    .


  5. Location
    PA, US
    Posts
    1,134
    your code
    Code:  
    1. void Sart ()

    what I think you want

    Code:  
    1. void Start()

    Now I do not have skype I dont like Skype but I had to break down and start using it


  6. Location
    Sammamish, WA
    Posts
    220
    Quote Originally Posted by kingcharizard View Post
    your code
    Code:  
    1. void Sart ()

    what I think you want

    Code:  
    1. void Start()
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •