Search Unity

" The name "FirstObject" doesn't exist in the current context " ERROR [SOLVED]

Discussion in 'Scripting' started by Polymath, Jul 2, 2015.

  1. Polymath

    Polymath

    Joined:
    Jun 28, 2015
    Posts:
    5
    I have three objects, and I want the first one to appear on the screen at the begining of the game.
    Then, when I click the mouse, I want the first one to dissapear and make the second one appear. On the next click, the second one to dissapear and the third to appear and next the same but dissapearing the third and appearing the first again. Again and again.
    The problem is that when I call the function Destroy(MyObject) it doesn't recognize the object.
    I create a variable called "condition" that I use to control when I want to create the first object, because as it is inside the update () it would create objects forever.
    Code (CSharp):
    1. //Code with errors
    2. public GameObject[] myGameObject;
    3.     private int condition=0;
    4.    
    5.     void Update () {
    6.         if (condition == 0) {
    7.             GameObject FirstObject = (GameObject)Instantiate (myGameObject [0], new Vector3 (0, 0, -4), Quaternion.identity);
    8.             condition =1;
    9.         }
    10.         if (Input.GetMouseButtonDown (0)) {
    11.             Destroy (FirstObject);
    12.             //Now I would create the next object
    13.             //GameObject SecondObject = (GameObject)Instantiate (myGameObject [1], new Vector3 (0, 0, -4), Quaternion.identity);
    14.         }
    15.     }
    As you can see the three objects are inside an array of objects.
    What I don't undestand is that if I create the object inside the if function of the click it doesn't give me the error, but I need it outside so it doesn't really matter.
    Code (CSharp):
    1. //Code without errors
    2. public GameObject[] myGameObject;
    3.     private int condition=0;
    4.    
    5.     void Update () {
    6.      
    7.         if (Input.GetMouseButtonDown (0)) {
    8.              GameObject FirstObject = (GameObject)Instantiate (myGameObject [0], new Vector3 (0, 0, -4), Quaternion.identity);
    9.             Destroy (FirstObject);
    10.  
    11.         }
    12.     }
     
  2. steego

    steego

    Joined:
    Jul 15, 2010
    Posts:
    969
    Because your GameObject FirstObject is inside the if block, it is local to this block and not visible to any code outside of it. Make it a member variable of the class instead, because you want to keep it from frame to frame as well.
     
    Polymath likes this.
  3. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
  4. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    As what steego said, but with an example just in case.
    Code (CSharp):
    1.     public GameObject[] myGameObject;
    2.     private int condition=0;
    3.     GameObject someObject;
    4.  
    5.     void Update () {
    6.         if (condition == 0) {
    7.             someObject = (GameObject)Instantiate (myGameObject [0], new Vector3 (0, 0, -4), Quaternion.identity);
    8.             condition =1;
    9.         }
    10.         if (Input.GetMouseButtonDown (0)) {
    11.             Destroy (someObject);
    12.             someObject = (GameObject)Instantiate (myGameObject [1], new Vector3 (0, 0, -4), Quaternion.identity);
    13.         }
    14.     }
     
    Polymath likes this.
  5. Polymath

    Polymath

    Joined:
    Jun 28, 2015
    Posts:
    5
    Thank you very much for your help guys!
    :)
     
    HiddenMonk likes this.