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

Using C# to destroy an object that technically does not exist...yet

Discussion in 'Scripting' started by kalacia, Jul 29, 2016.

  1. kalacia

    kalacia

    Joined:
    Apr 25, 2015
    Posts:
    7
    Im getting the error "houses10 does not exist in the current context".

    Im working on the assumption that's because the object/var/gameobject does not exist yet. As it gets defined in the previous IF statement. I intend to end up with a lot of gameobjects similar to this, so i don't want to make lots of objects in the GUI and hide them, my GUI would be filled with hundreds of images/sprites.. also id like to randomise the images used. Hence the array, that's for later use. Id like the gameobjects to be dynamically created and destroyed, if possible.

    Would i need to create an array of all the possible gameobjects and reference that?

    Im a bit stumped on how to remove a subjectively created gameobject. Although the code below shows enabled. (been playing with that to get the syntax correct. Dont want to use destroy untill im sure ill get it correct).

    Thanks in advance guys, gals and comrades.



    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class SpriteControl : MonoBehaviour {
    5.  
    6.     public GameVars gamevars;
    7.     public Sprite[] homeSprites;
    8.        
    9.     // Use this for initialization
    10.     void Start () {
    11.        
    12.         homeSprites = Resources.LoadAll<Sprite>("house1");
    13.    
    14.     }
    15.    
    16.     public void Endturn(){
    17.        
    18.         //houses built
    19.         if (gamevars.homesbuilt >= 10){
    20.                    
    21.         GameObject houses10 = new GameObject();
    22.         houses10.AddComponent<SpriteRenderer>();
    23.         houses10.GetComponent<SpriteRenderer>().sprite = homeSprites[0];
    24.         houses10.transform.Translate(-125, -253, 0);
    25.         houses10.transform.localScale = new Vector3(85f, 85f, 0f);
    26.         houses10.name = "sprite_house10";
    27.            
    28.         }
    29.    
    30.        
    31.         if (gamevars.homesbuilt < 10 && GameObject.Find("sprite_house10")){
    32.            
    33.             houses10.enabled = false;
    34.            
    35.         }
    36.            
    37.     }
    38.    
    39. }
     
    Last edited: Jul 29, 2016
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,384
    Why not just keep track of all of the houses as they're created? That's what List<T> is for.

    Also, use code tags.
     
  3. kalacia

    kalacia

    Joined:
    Apr 25, 2015
    Posts:
    7
    the house sprite is created at 10 houses (var for housesbuilt stored in another cs file). As each housing sprite will occur at larger exponential intervals. The sprite itself in this code has no bearing on the actual maths of the game itself. Its just a visual reward for the user.

    I want to remove the housing sprite if the houses drips below 10. The same logic/syntax would be used for lumber mills, quarries, markets etc. Just different positions.
     
  4. LeftyRighty

    LeftyRighty

    Joined:
    Nov 2, 2012
    Posts:
    5,148
    the error you're getting is because of scope.

    Code (csharp):
    1.  
    2. scopeblock
    3. {
    4. declare variable X
    5. // x exists here
    6. }
    7. //x doesn't exist here
    8.  
    9. declare variable Y
    10. scopeblock
    11. {
    12. // y exists here
    13. }
    14. // y exists here
    15.  


    Code (csharp):
    1.  
    2. GameObject houses10 =newGameObject();
    3. houses10.name="sprite_house10";
    4. //...
    5. GameObject.Find("sprite_house10")
    6.  
    this is poor, why use find (not cheap function) when you already have a reference to the thing?

    you might find just moving the declaration so it's in a better scope will help
    Code (csharp):
    1.  
    2. //houses built
    3. GameObject houses10; // default should be null if not set it to null here instead
    4. if (gamevars.homesbuilt >= 10)
    5. {
    6.     houses10 = new GameObject();
    7.     houses10.AddComponent<SpriteRenderer>();
    8.     houses10.GetComponent<SpriteRenderer>().sprite = homeSprites[0];
    9.     houses10.transform.Translate(-125, -253, 0);
    10.     houses10.transform.localScale = new Vector3(85f, 85f, 0f);
    11.     houses10.name = "sprite_house10";
    12. }
    13.  
    14. if (gamevars.homesbuilt < 10 && houses10 != null) // check the ref isn't still null
    15. {
    16.     houses10.enabled = false;
    17. }
    18.  
     
  5. kalacia

    kalacia

    Joined:
    Apr 25, 2015
    Posts:
    7
    @LeftyRighty

    Thanks for that, got it. Was down to the scope being incorrect as you said. Im used to javascript and its global vars/objects.