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 Ty1ensen · Feb 04, 2014 at 01:45 AM · errorloop

how to loop a script

 function Start () {
 Repeat();
 
 function Repeat();
     gameObject.tag = "Army Cube";
     yield WaitForSeconds(1);
     gameObject.tag = "Ice Cube";
     yield WaitForSeconds(1); 
     gameObject.tag = "Fire Cube";
     yield WaitForSeconds(1);
     gameObject.tag = "Mini Cube"; 
     yield WaitForSeconds(1);
     gameObject.tag = "Gravity Cube";   
     }

i have this code its suppose to loop but i keep getting this Error

.js(4,10): BCE0044: expecting (, found 'Repeat'.

i did what it said but that just adds more errors i don't know what to do

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 ffxz7ff · Feb 04, 2014 at 01:47 AM 1
Share

I find it very unlikely that you could make a game without knowing how loops work. Go read some program$$anonymous$$g tutorials, that will also let you fix your syntax errors.

avatar image Ty1ensen · Feb 04, 2014 at 02:32 AM 0
Share

well sir i am only 15

avatar image Pigifying · Feb 04, 2014 at 02:47 AM 0
Share

http://unity3d.com/learn/tutorials/modules/beginner/scripting/loops

Every loopy thing you need to know

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by iwaldrop · Feb 04, 2014 at 04:01 AM

It looks to me like you're trying to create a coroutine, and not a simple for loop. Coroutines are pretty simple too though, so perhaps you mean to be doing the following:

 function Start ()
 {
     Repeat();
 }
 
 function Repeat()
 {
     while (true)
     {
         gameObject.tag = "Army Cube";
         yield WaitForSeconds(1);
         gameObject.tag = "Ice Cube";
         yield WaitForSeconds(1);
         gameObject.tag = "Fire Cube";
         yield WaitForSeconds(1);
         gameObject.tag = "Mini Cube";
         yield WaitForSeconds(1);
         gameObject.tag = "Gravity Cube";
         yield WaitForSeconds(1);
     }
 }

There are a lot of better was to write this though, like by using a modulus operation (written in C#, but you can copy/paste the whole codeblock into a file called LoopingTagAssigner.cs):

 using UnityEngine;
 using System.Collections;
 
 public class LoopingTagAssigner : MonoBehaviour
 {
     public float timer = 1;
     public string[] titles = new string[5]
     {
         "Army Cube",
         "Ice Cube",
         "Fire Cube",
         "Mini Cube",
         "Gravity Cube",
     };
 
     private int index;
 
     void OnEnable()
     {
         StartCoroutine(UpdateTitle());
     }
 
     IEnumerator UpdateTitle()
     {
         YieldInstruction wait = new WaitForSeconds(timer);
         while (enabled)
         {
             gameObject.tag = titles[index];
             index = ++index % titles.Length;
             yield return wait;
         }
     }
 }

The advantages to using the above method is that the tags that you're cycling through don't need to be hardcoded in the script. However, an even better method would be to use a component to track the value instead of setting the tag (because there are a finite number of tags, and a game object can only have one tag at a time).

So you could try doing something like this:

 using UnityEngine;
 using System.Collections;
 
 public class CubeType : MonoBehaviour
 {
     public enum Type
     {
         Army,
         Ice,
         Fire,
         Mini,
         Gravity,
     }
 
     public Type CurrentType { get; private set; }
 
     void Awake()
     {
         numberOfTypes = System.Enum.GetValues(typeof(Type)).Length;
     }
 
     private int numberOfTypes;
     private int index;
 
     public Type CycleType()
     {
         CurrentType = (Type)System.Enum.ToObject(typeof(Type), index);
         index = ++index % numberOfTypes;
         return CurrentType;
     }
 }
 
 using UnityEngine;
 using System.Collections;
 
 [RequireComponent(typeof(CubeType))]
 public class LoopingTagAssigner : MonoBehaviour
 {
     public float timer = 1;
 
     private CubeType cubeType;
     private int index;
 
     void Awake()
     {
         cubeType = GetComponent<CubeType>();
     }
 
     void OnEnable()
     {
         enabled = cubeType != null;
         if (enabled)
             StartCoroutine(UpdateTitle());
     }
 
     IEnumerator UpdateTitle()
     {
         YieldInstruction wait = new WaitForSeconds(timer);
         while (enabled)
         {
             name = string.Format("{0} Cube", cubeType.CycleType().ToString());
             yield return wait;
         }
     }
 }

I'm setting the name (purely so that you can see something happening when you hit play). You can query the current type easily by doing,

 GetComponent<CubeType>().CurrentType;

All in all, these are contrived examples that demonstrate how you can use a couple of nice features. I wrote them in C# because I hate unityscript. :)

Comment
Add comment · 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
0

Answer by cassius · Feb 04, 2014 at 02:13 AM

Your function named "function Repeat();" is located inside your Start() function. Notice the "Start() {" opens the Start function but it does not appear to close with "}" - at least not in the code you have shown.

Maybe add the "}" on the line just after "function Repeat();", so it would look like this:

 function Start () {
     Repeat();
 }
 function Repeat() {
     gameObject.tag = "Army Cube";
     yield WaitForSeconds(1);
     gameObject.tag = "Ice Cube";
     yield WaitForSeconds(1);
     gameObject.tag = "Fire Cube";
     yield WaitForSeconds(1);
     gameObject.tag = "Mini Cube";
     yield WaitForSeconds(1);
     gameObject.tag = "Gravity Cube";
 }

That said, I think you may need to do a bit more reading to get a better grasp on this.

Edited to add that you're also closing the Repeat() function before it ever gets started by adding the ";" right after it. see above sourcecode

Comment
Add comment · 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

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

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

getting loop errors, need help 1 Answer

Increase value through frames, or in a while? 1 Answer

Instantiating children in loop not working 2 Answers

Unity3D MovieTexture Loop 0 Answers

Using arrays and for loop with GetComponent error 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