Search Unity

Respawning GameObject that Disappears on click, but also respawns again.

Discussion in 'Scripting' started by colbyjacob8, Nov 29, 2017.

  1. colbyjacob8

    colbyjacob8

    Joined:
    Nov 28, 2017
    Posts:
    10
    I'm creating a simple game where box's fall from the screen and you have to click on them to disappear and then respawn to complete the same task. Currently I'm having an issue with destroying or causing the box's that have been clicked to disappear. The first clone of the box that i've been using is the only one that gets destroyed, all the other clones don't seem to be effected by the (transform/localScale) code. Also im only working on a certain cube currently and then ill spread to all the rest.
    New to posting here so sorry if i forgot anything important. Thanks!
    Heres the code that I've created so far.
    Code (CSharp):
    1.  
    2.     public GameObject ball;
    3.  
    4.     public Transform spawnPos;
    5.  
    6.     public float spawnTime = 1f;
    7.     // Use this for initialization
    8.     void Start()
    9.     {
    10.         spawnTime = Random.Range(1f, 3f);
    11.         InvokeRepeating("SpawnBall", spawnTime, spawnTime);
    12.     }
    13.    
    14.     void Update()
    15.     {
    16.         if (Input.GetMouseButtonDown(0))
    17.         {
    18.          
    19.             RaycastHit hit;
    20.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    21.  
    22.             if (Physics.Raycast(ray, out hit))
    23.             {
    24.                 if (hit.transform.tag == "Cube")
    25.                 {
    26.                  
    27.                     GameObject.Find("Cube (4)").transform.localScale = new Vector3(0, 0, 0);
    28.                
    29.  
    30.                 }
    31.                
    32.             }
    33.         }
    34.     }
    35.     void SpawnBall()
    36.     {
    37.        
    38.         GameObject clonedObject = Instantiate(ball, spawnPos.position, spawnPos.rotation);
    39.         clonedObject.name = ball.name;
    40.     }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    I would suggest that you use the IPointerClickHandler interface. Look it up, it's pretty easy to use and will work nicely for you.
    It goes on every cube, and just calls a method when you click on it.
    It would require changing a few things.

    Another option you could use, is to change your game object Find code to be the hit.gameObject.
    Right now, you're using GameObject.Find and looking for essentially the first game object the engine returns as found.. that's why it's "only working for one", you think/observe :)
     
  3. colbyjacob8

    colbyjacob8

    Joined:
    Nov 28, 2017
    Posts:
    10
    Thank you for responding! Would the hit.gameObject work as in hit.box.transform ect...? As im trying it that way and its saying that RayCastHit doesn't contain a definition for box or gameObject. Thank you again.
     
  4. colbyjacob8

    colbyjacob8

    Joined:
    Nov 28, 2017
    Posts:
    10
    I Figured it out thank you for the help!
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool :) Ya, sorry, I guess maybe you used: hit.transform.gameObject or something similar.

    You're welcome.