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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoaderGamejolt : MonoBehaviour {
void Start () {
StartCoroutine("Fail")
GameObject goMyObject = GameObject.Find("ErrorMsg");
goMyObject.SetActiveRecursively(false);
}
IEnumerator Fail(){
yield return new WaitForSeconds(10);
goMyObject.SetActiveRecursively(true);
}
}
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.
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: 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.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoaderGamejolt : MonoBehaviour {
void Start () {
StartCoroutine(Fail());
GameObject goMyObject = GameObject.Find("ErrorMsg");
goMyObject.SetActive(false);
}
IEnumerator Fail(){
yield return new WaitForSeconds(10);
goMyObject.SetActive(true);
}
}
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
Okay, that’s the one I said you have to move the declaration to the class level.
[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.
GameObject goMyObject; // declared outside of 'Start', at the class level (as opposed to local, if it were in Start)
void Start() {
goMyObjec = GameObject.Find("ErrorMsg");
// ... etc all the rest of your code
}
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’
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoaderGamejolt : MonoBehaviour {
[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.
GameObject goMyObject; // declared outside of 'Start', at the class level (as opposed to local, if it were in Start)
void Start() {
goMyObject = GameObject.Find("ErrorMsg");
StartCoroutine(Fail());
GameObject goMyObject = GameObject.Find("ErrorMsg");
goMyObject.SetActive(false);
}
IEnumerator Fail(){
yield return new WaitForSeconds(10);
GameObject goMyObject = GameObject.Find("ErrorMsg");
goMyObject.SetActive(true);
}
}
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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LoaderGamejolt : MonoBehaviour {
[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.
GameObject goMyObject; // declared outside of 'Start', at the class level (as opposed to local, if it were in Start)
void Start() {
goMyObject = GameObject.Find("ErrorMsg");
if(goMyObject == null){
print("Error! Couldn't find the game object.");
return;
}
StartCoroutine(Fail());
goMyObject.SetActive(false);
}
IEnumerator Fail(){
yield return new WaitForSeconds(10);
goMyObject.SetActive(true);
}
}
I added a console debug log message if it doesn’t find the game object.
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)
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.