Search Unity

RollABall Add on

Discussion in 'Scripting' started by roger4325, Jul 26, 2018.

  1. roger4325

    roger4325

    Joined:
    Jul 17, 2018
    Posts:
    3
    So I am learning about how to code and everything and have been following the tutorials of RollABall and also the Space Shooter. I have finished both of these tutorials and have decided to learn a bit more and try to implement new features. So I want to make it so you can score a 100 points in RollABall before winning by making the objects re spawn so you can keep collecting them. In the void OnTriggerEnter I want to call a corountine and wait for a set amount of seconds. Then I want to do other.gameObject.SetActive(true);
    however it says other doesn't exist. I'm not sure what i am doing wrong. Any help would be greatly appreciated.
    Thank you
    void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("Collectables"))
    {
    other.gameObject.SetActive(false);
    count = count + 1;
    SetCountText();
    StartCoroutine(Respawn());
    }
    }

    IEnumerator Respawn()
    {
    yield return new WaitForSeconds(respawn);
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Not sure what you mean by "it says other doesn't exist". Are you getting an error? If so, what is the specific error, and what line of code is it pointing to?
     
  3. roger4325

    roger4325

    Joined:
    Jul 17, 2018
    Posts:
    3
    After the yield return new wait for seconds, I want to call other.gameObject.setActive(true).
    As I did in the lines above to set it to false.However I get an error that points to the other part of the game object saying this reference doesn't exist. I'm not sure how I'm supposed to call my gameObjects with tag "collectables" like I did above.
    Thank you for your assistance
     
  4. Doug_B

    Doug_B

    Joined:
    Jun 4, 2017
    Posts:
    1,596
    That object does not exist in the context of the Respawn method. Try this :-
    Code (CSharp):
    1.     void OnTriggerEnter(Collider other)
    2.     {
    3.         if (other.gameObject.CompareTag("Collectables"))
    4.         {
    5.             other.gameObject.SetActive(false);
    6.             count = count + 1;
    7.             SetCountText();
    8.             StartCoroutine(Respawn(other.gameObject));
    9.         }
    10.     }
    11.  
    12.     IEnumerator Respawn(GameObject go)
    13.     {
    14.         yield return new WaitForSeconds(respawn);
    15.         go.SetActive(true);
    16.     }
     
  5. roger4325

    roger4325

    Joined:
    Jul 17, 2018
    Posts:
    3
    [QUOTE="Doug_B, post:] Thank you so much it worked like a charm!
     
    Doug_B likes this.