Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JA_555 · Oct 17, 2014 at 12:18 AM · resetrestart

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.

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MrSoad · Oct 17, 2014 at 12:33 AM 0
Share

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?

avatar image JA_555 · Oct 17, 2014 at 01:20 AM 0
Share

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

avatar image Kiwasi · Oct 17, 2014 at 01:50 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
2

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Anxo · Oct 17, 2014 at 12:32 AM 0
Share

called like so StartCoroutine("RandomLoop");

avatar image Kiwasi · Oct 17, 2014 at 01:51 AM 0
Share

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.

avatar image JA_555 · Oct 17, 2014 at 02:14 AM 0
Share

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");
 }}
avatar image Anxo · Oct 19, 2014 at 07:42 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges