- Home /
Restart a function
The is the script I'm using. The purpose is to choose a random object, and after 7 seconds disable the mesh. What I WANT to happen is after 7 seconds, it restarts this process all over again but that isn't working.
using UnityEngine;
using System.Collections;
public class random : MonoBehaviour {
GameObject[] spawnPoints;
GameObject currentPoint;
int index;
public float minWaitTime = 3.0f;
public float maxWaitTime = 5.0f;
public MeshRenderer shape;
public MeshRenderer shapetwo;
void Awake(){
GameObject.Find ("Cube").GetComponent<MeshRenderer> ().enabled = false;
GameObject.Find ("Sphere").GetComponent<MeshRenderer> ().enabled = false;
}
IEnumerator Start()
{
yield return new WaitForSeconds(Random.Range(minWaitTime, maxWaitTime));
shape = GameObject.Find("Cube").GetComponent<MeshRenderer>();
shapetwo = GameObject.Find("Sphere").GetComponent<MeshRenderer>();
spawnPoints = GameObject.FindGameObjectsWithTag("Shapes");
index = Random.Range (0, spawnPoints.Length);
currentPoint = spawnPoints[index];
print (currentPoint.name);
yield return new WaitForSeconds (7);
hinge.enabled = false;
hingetwo.enabled = false;
Start ();
}
IEnumerator Example(){
yield return new WaitForSeconds (15);{
Start();}
}
//void exampletwo(){
//InvokeRepeating ("Start", 0, 15F);
//InvokeRepeating ("Exampletwo", 0, 15F);
//}
void Update(){
Start ();
}
}
I looked though this http://answers.unity3d.com/questions/371797/restart-this-script.html and I thought that maybe I could add something in Update but I'm not sure what to do.
I also looked at http://docs.unity3d.com/ScriptReference/MonoBehaviour.Reset.html, but it says it is only called in editor mode, so I don't think that helps. Let me know what I can do, Thanks.
Which bit is not working?
Why do you want to do this in Start(). Could you not put all this in a separate function?
Also could you not store the "Cube" and "Sphere" $$anonymous$$eshRenderer components when you first find them in Awake() into "shape" and "shapetwo" respectively. It looks like you are doing twice as much work as you need on this bit.
Is this code compiling at the moment, if not what errors are you getting?
I don't think any of that matters, I'm asking how I can run this like a loop fashion. Putting it in the Start function and doing twice as much work does not seem to be the problem
Recursive functions without a ter$$anonymous$$ation condition is just asking for a stack overflow. (There might be some coroutine magic going on here that's saving your bacon)
Calling Start, or any of the other Unity reserved functions is also a bad idea. Very quickly leads to messy code.
Answer by Anxo · Oct 17, 2014 at 12:30 AM
If Start is an Ienumerator, it has to be started using "StartCoroutine" instead of calling it via Start(); But if you want it to continue to loop you might as well just do something like.
bool runLoop = true;
IEnumarator RandomLoop(){
while (runLoop){
//do something
yield return new WaitForSeconds (15);
// Here it will go back up to under the "while" loop.
}
}
WUCC
If you want infinite looping you can change your condition to
while (true)
Unity will stop running the coroutine when the calling $$anonymous$$onoBehaviour is destroyed.
Here is my new code, what did I do wrong?
using UnityEngine;
using System.Collections;
public class random : $$anonymous$$onoBehaviour {
GameObject[] spawnPoints;
GameObject currentPoint;
int index;
public float $$anonymous$$WaitTime = 3.0f;
public float maxWaitTime = 5.0f;
public $$anonymous$$eshRenderer shape;
public $$anonymous$$eshRenderer shapetwo;
bool runLoop = true;
void Awake(){
GameObject.Find ("Cube").GetComponent<$$anonymous$$eshRenderer> ().enabled = false;
GameObject.Find ("Sphere").GetComponent<$$anonymous$$eshRenderer> ().enabled = false;
}
IEnumerator RandomLoop(){
while (true){
yield return new WaitForSeconds(Random.Range($$anonymous$$WaitTime, maxWaitTime));
shape = GameObject.Find("Cube").GetComponent<$$anonymous$$eshRenderer>();
shapetwo = GameObject.Find("Sphere").GetComponent<$$anonymous$$eshRenderer>();
spawnPoints = GameObject.FindGameObjectsWithTag("Shapes");
index = Random.Range (0, spawnPoints.Length);
currentPoint = spawnPoints[index];
print (currentPoint.name);
yield return new WaitForSeconds (7);
hinge.enabled = false;
hingetwo.enabled = false;
yield return new WaitForSeconds (15);
// Here it will go back up to under the "while" loop.
StartCoroutine("RandomLoop");
}}
you need to move your "StartCoroutine" outside of itself. Right now "RandomLoop will never be called because you start it no where but in itself. Also, move Shape and shape 2 "Finds" outside the loop so it does not have to look for them all the time, ... Also, using FindwithTag is much faster than Find but you have to set up tags for and assign them first as you do with the spawnpoints.
void Awake(){
GameObject.Find ("Cube").GetComponent<$$anonymous$$eshRenderer> ().enabled = false;
GameObject.Find ("Sphere").GetComponent<$$anonymous$$eshRenderer> ().enabled = false;
// this is where you are starting the loop but only once, you do not have to call it again. That is how "while" works, it will continue to do something till the condition inside the () is false
StartCoroutine("RandomLoop");
}
IEnumerator RandomLoop(){
shape = GameObject.Find("Cube").GetComponent<$$anonymous$$eshRenderer>();
shapetwo = GameObject.Find("Sphere").GetComponent<$$anonymous$$eshRenderer>();
spawnPoints = GameObject.FindGameObjectsWithTag("Shapes");
while (true){
// you are siting for a random amount of time.
yield return new WaitForSeconds(Random.Range($$anonymous$$WaitTime, maxWaitTime));
// you are asigning some random spawn point
index = Random.Range (0, spawnPoints.Length);
currentPoint = spawnPoints[index];
print (currentPoint.name);
// waiting again, this time for 7 seconds
yield return new WaitForSeconds (7);
// now you are turning stuff off
hinge.enabled = false;
hingetwo.enabled = false;
// now you are waiting again for 15seconds before WAITING AGAIN FOR RANDO$$anonymous$$ A$$anonymous$$OUNT OF TI$$anonymous$$E AT THE TOP OF THE LOOP
yield return new WaitForSeconds (15);
// Here it will go back up to under the "while" loop.
}}
For you to better understand how a while loop works, you should run this code and watch output.
Void Start(){
StartCoroutine("CountTillTen");
}
IEnumarator CountTillTen(){
int count = 0;
while (count < 10){
yield return new WaitForSeconds(1);
count ++;
Debug.Log(count);
}
}
WUCC
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Restart Mecanim's Animator controller 3 Answers
Restart the game when Health variable reaches 0 4 Answers
How to restart/rerun a script? 2 Answers
Reset button 1 Answer