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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by Ebamber · Dec 02, 2013 at 01:22 PM · javascriptcounterunderstanding logic

Logical Error In Spawn Counter (Javascript)

 var home: GameObject;
 var activeSpawn: int;
 var countDown: float;
 function Start(){
     switch(home.name)
     {
         case "SP1": activeSpawn=0;
         case "SP2": activeSpawn=1;
         case "SP3": activeSpawn=2;
         case "SP4": activeSpawn=3;
         case "SP5": activeSpawn=4;
         case "SP6": activeSpawn=5;
     }
     countDown=30*(activeSpawn+1);
 }

The above is a sample of the relevant code in my script.

I'm trying to make a spawn counter for this game I'm making but for some reason every spawn location is treated as SP6 If you hadn't noticed my Spawn Points are SP 1-6, and home is the game object in which the script is contained each time (that's also something I was hoping for some enlightenment on, is there any better way to let the script know of the game object it's contained in?

countDown is the timer for each spawn, and activeSpawn is supposed to be the number of "active" spawn points - the logic I used was that for SP1 to be active, I'd need to have 0 active spawn points and wait 30 seconds, for SP2 I'd need to have 1 active spawn point and wait another 30 seconds (meaning 30 seconds for SP1 and 30 more seconds for SP2) and so on for the rest.

That's all the detail that came to mind if you need anything else to help me let me know

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

2 Replies

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

Answer by bompi88 · Dec 02, 2013 at 01:28 PM

You are missing some breaks inside your switch statements. To let the compiler know when to jump out of the case you have to put break; after it like so:

    case "SP1": activeSpawn=0; break; // treats this as one case
    case "SP2": activeSpawn=1;
    case "SP3": activeSpawn=2;
    case "SP4": activeSpawn=3; break; // jumps out here, but treats SP2, SP3 and SP4 as one case.

So in your script you want to treat each case differently, which results in a break; after each case.

 var home: GameObject;
 var activeSpawn: int;
 var countDown: float;
 function Start(){
     switch(home.name)
     {
        case "SP1": activeSpawn=0; break;
        case "SP2": activeSpawn=1; break;
        case "SP3": activeSpawn=2; break;
        case "SP4": activeSpawn=3; break;
        case "SP5": activeSpawn=4; break;
        case "SP6": activeSpawn=5; break;
     }
     countDown=30*(activeSpawn+1);
 }

But, you could also do something different, based on your name pattern of your object. If you know that all spawn points you are going to use have "SP" as a prefix and the countDown variable is dependent on the number in your objects name, you could replace your switch with something like:

 var activeSpawn : int = System.Int32.Parse(home.name.Substring(2));
 countDown = 30 * activeSpawn;
Comment
Add comment · Show 2 · 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 Ebamber · Dec 02, 2013 at 01:35 PM 0
Share

Thanks! I can't believe the answer was so obvious, I had tried break statements by putting them along with the activeSpawn initialization inside a code block and was getting an error for it, I guess my mistake in that case was the code block not the actual break statements... I still need to get used to Javascript, it's similar enough to Java, but there's still some things about it that I don't quite get right =/ Anyway, thanks! :)

avatar image bompi88 · Dec 02, 2013 at 01:42 PM 0
Share

I think the second option I posted will work too, but in only one line.

avatar image
0

Answer by anderas · Dec 02, 2013 at 01:53 PM

Just to make sure: you have the right "home" object set in every script in the inspector, right?

Regarding your question about alternatives: In C#, you can access the GameObject that your script is attached to with the member variable "gameObject". I'm sure you can do it in JS, too :)

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

18 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

Related Questions

Dynamically Spawning Trees Based On The Viability of the Landscape? 0 Answers

GUI Help, timer and counter 1 Answer

How to give a bonus every 1000 points? 2 Answers

Dual Timer Problems 1 Answer

Using Time.deltaTime as time counter. 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