Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Help] Unable to make an object disappear and then reappear after a few seconds

Discussion in 'Scripting' started by oggycather, Feb 11, 2018.

  1. oggycather

    oggycather

    Joined:
    Feb 11, 2018
    Posts:
    9
    Can anyone tell me what am i doing wrong? I always get an error (Assets/LoaderGamejolt.cs(10,1): error CS1525: Unexpected symbol `GameObject')
    (I am pretty new to unity and C#)

    I need to know how to make this script work, anything would do as long as it works.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LoaderGamejolt : MonoBehaviour {
    6.  
    7.  
    8.     void Start () {
    9.     StartCoroutine("Fail")
    10.        GameObject goMyObject = GameObject.Find("ErrorMsg");
    11.         goMyObject.SetActiveRecursively(false);
    12.  
    13.  
    14.     }
    15.  
    16.     IEnumerator Fail(){
    17.         yield return new WaitForSeconds(10);
    18.         goMyObject.SetActiveRecursively(true);
    19.              
    20. }
    21.  
    22.  
    23. }
    24.  
     
    Last edited: Feb 11, 2018
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, you have 3 problems, at least. One is that you missed a semi colon on the line where you begin your coroutine.

    Next is that the game object you're trying to disable/enable needs to have a class level reference. Right now, you have a local variable and a non-existent one (in Start/Fail).

    Lastly, unless you're on an older version of Unity, try switching to (just) 'SetActive' rather than the SetActiveRecursively that you are using.


    You can (should prefer?) to use the non-string version of StartCoroutine: StartCoroutine(Fail()); like that.

    If that game object this script is on & the one you're trying to find are both in the scene, you should try linking it in the inspector, by making the variable public or using the 'SerializeField' attribute.

    I hope that helps :)
     
  3. oggycather

    oggycather

    Joined:
    Feb 11, 2018
    Posts:
    9
    Thank you for your reply, could you send me an example script? (I am running the 2018 Beta version of Unity)
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, I listed everything. Did you try my suggestions? How much of what I said was confusing?

    If none of it made sense, you should really try the very basics of learning Unity & scripting (in Unity).

    There are some good subsections here: https://unity3d.com/learn
    (unity essentials , scripting) .. as well as 1 or 2 very small game tutorials : Roll-A-Ball, space shooter (and more). I suggest that you try following along with those, watching and writing the code yourself, to get familiar with everything. :)
     
  5. oggycather

    oggycather

    Joined:
    Feb 11, 2018
    Posts:
    9
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class LoaderGamejolt : MonoBehaviour {
    5.  
    6.     void Start () {
    7.     StartCoroutine(Fail());
    8.        GameObject goMyObject = GameObject.Find("ErrorMsg");
    9.         goMyObject.SetActive(false);
    10.     }
    11.     IEnumerator Fail(){
    12.         yield return new WaitForSeconds(10);
    13.         goMyObject.SetActive(true);
    14.            
    15. }
    16. }
    17.  
    I did change it a bit but i still get the same error.. Assets/LoaderGamejolt.cs(17,9): error CS0103: The name `goMyObject' does not exist in the current context
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay, that's the one I said you have to move the declaration to the class level.
    Code (csharp):
    1.  
    2. [SerializeField] // if you add this attribute, you can drag n drop the game object "ErrorMsg" in the inspector and delete the line for "GameObject.Find" and it will work.
    3. GameObject goMyObject; // declared outside of 'Start', at the class level (as opposed to local, if it were in Start)
    4.  
    5. void Start() {
    6.    goMyObjec = GameObject.Find("ErrorMsg");
    7.  // ... etc all the rest of your code
    8.  }
    Try that.
     
  7. oggycather

    oggycather

    Joined:
    Feb 11, 2018
    Posts:
    9
    Ok tried it... still not working but i got a new error:
    Assets/LoaderGamejolt.cs(10,4): error CS0844: A local variable `goMyObject' cannot be used before it is declared. Consider renaming the local variable when it hides the member `LoaderGamejolt.goMyObject'
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class LoaderGamejolt : MonoBehaviour {
    5. [SerializeField] // if you add this attribute, you can drag n drop the game object "ErrorMsg" in the inspector and delete the line for "GameObject.Find" and it will work.
    6. GameObject goMyObject; // declared outside of 'Start', at the class level (as opposed to local, if it were in Start)
    7. void Start() {
    8.    goMyObject = GameObject.Find("ErrorMsg");
    9.    StartCoroutine(Fail());
    10.       GameObject goMyObject = GameObject.Find("ErrorMsg");
    11.         goMyObject.SetActive(false);
    12.     }
    13.     IEnumerator Fail(){
    14.         yield return new WaitForSeconds(10);
    15. GameObject goMyObject = GameObject.Find("ErrorMsg");
    16.         goMyObject.SetActive(true);
    17.          
    18.   }
    19.    }
    20.  
     
    Last edited: Feb 11, 2018
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    There's no need to redeclare that so many times.
    If you linked it in the inspector, remove the line with GameObject find from the following code:
    Code (csharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. public class LoaderGamejolt : MonoBehaviour {
    6. [SerializeField] // if you add this attribute, you can drag n drop the game object "ErrorMsg" in the inspector and delete the line for "GameObject.Find" and it will work.
    7. GameObject goMyObject; // declared outside of 'Start', at the class level (as opposed to local, if it were in Start)
    8. void Start() {
    9.    goMyObject = GameObject.Find("ErrorMsg");
    10.    if(goMyObject == null){
    11.        print("Error!  Couldn't find the game object.");
    12.        return;
    13.      }
    14.    StartCoroutine(Fail());
    15.    goMyObject.SetActive(false);
    16.     }
    17.     IEnumerator Fail(){
    18.         yield return new WaitForSeconds(10);
    19.         goMyObject.SetActive(true);    
    20.   }
    21.    }
    I added a console debug log message if it doesn't find the game object.
     
  9. oggycather

    oggycather

    Joined:
    Feb 11, 2018
    Posts:
    9
    Ok i tried it, no errors or any debug messages, the error box does disappear but it just does not reappear after 10 seconds
    and is that what you mean by linking? (the red box, the script is in the camera)
     
    Last edited: Feb 11, 2018
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Alright, well make sure that the game object that you're disabling is not the same (or a parent) object to the one that has the script... otherwise the coroutine will stop running.

    What I mean by "linking" is drag n drop in the inspector. You can see the variable on the script we've been working on , in the inspector. You can drag the game object, from the hierarchy, into the variable slot.
     
  11. oggycather

    oggycather

    Joined:
    Feb 11, 2018
    Posts:
    9
    Thank you so much for helping i got it to work, i just didn't understand what you meant by linking before but now i do thanks again
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    No problem. Make sure if you did the "linking" that you remove the 'GameObject.Find' line of code, as it's no longer needed. :) Take care.
     
  13. oggycather

    oggycather

    Joined:
    Feb 11, 2018
    Posts:
    9
    Alright will do :)