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 Digital-Phantom · Jan 07, 2015 at 08:22 AM · javascriptrandomspawnloop

Only 2 out of 4 Objects ever spawn... Why ?(Closed)

I'm using the following script to spawn my weapons in game. I have 4 weapons and 4 spawn locations. A weapon will always spawn in all 4 locations BUT its only ever 'wep03' or 'wep04' never 'wep01' or 'wep02' I must have ran it 60-70 times (no exaggeration) and never had anything but the same 2 weapons.

 //Weapon prefabs to spawn
 var wep01 : GameObject;
 var wep02 : GameObject;
 var wep03 : GameObject;
 var wep04 : GameObject;
 
 //Spawn points
 var spawnPoint1 : Transform;
 var spawnPoint2 : Transform;
 var spawnPoint3 : Transform;
 var spawnPoint4 : Transform;
 
 function Start()
 {
     SpawnWeapons(); // Run SpawnWeapons coroutine
 }
 
 function SpawnWeapons()
 {
     for(var i=0;i<4;i++) // Run loop 4 times
     {
         var random1 : int = Random.Range(0,4);
 
         switch (random1)
         {
             case (0):
             var spawnWep1 = Instantiate(wep01);
             switch (i)
         {
             case (0):
             spawnWep1.transform.position = new Vector3(spawnPoint1.transform.position.x, spawnPoint1.transform.position.y, spawnPoint1.transform.position.z);
             spawnWep1.transform.tag = "Weapon01";  
             break;
             case (1):
             spawnWep1.transform.position = new Vector3(spawnPoint2.transform.position.x, spawnPoint2.transform.position.y, spawnPoint2.transform.position.z);
             spawnWep1.transform.tag = "Weapon02";  
             break;
             case (2):
             spawnWep1.transform.position = new Vector3(spawnPoint3.transform.position.x, spawnPoint3.transform.position.y, spawnPoint3.transform.position.z);
             spawnWep1.transform.tag = "Weapon03";  
             break;
             case (3):
             spawnWep1.transform.position = new Vector3(spawnPoint4.transform.position.x, spawnPoint4.transform.position.y, spawnPoint4.transform.position.z);
             spawnWep1.transform.tag = "Weapon04";  
             break;    
         }
             case (1):
             var spawnWep2 = Instantiate(wep02);
             switch (i)
         {            
             case (0):
             spawnWep2.transform.position = new Vector3(spawnPoint1.transform.position.x, spawnPoint1.transform.position.y, spawnPoint1.transform.position.z);
             spawnWep2.transform.tag = "Weapon01";  
             break;
             case (1):
             spawnWep2.transform.position = new Vector3(spawnPoint2.transform.position.x, spawnPoint2.transform.position.y, spawnPoint2.transform.position.z);
             spawnWep2.transform.tag = "Weapon02";  
             break;
             case (2):
             spawnWep2.transform.position = new Vector3(spawnPoint3.transform.position.x, spawnPoint3.transform.position.y, spawnPoint3.transform.position.z);
             spawnWep2.transform.tag = "Weapon03";  
             break;
             case (3):
             spawnWep2.transform.position = new Vector3(spawnPoint4.transform.position.x, spawnPoint4.transform.position.y, spawnPoint4.transform.position.z);
             spawnWep2.transform.tag = "Weapon04";  
             break;
         }
             case (2):
             var spawnWep3 = Instantiate(wep03);
             switch (i)
         {            
             case (0):
             spawnWep3.transform.position = new Vector3(spawnPoint1.transform.position.x, spawnPoint1.transform.position.y, spawnPoint1.transform.position.z);
             spawnWep3.transform.tag = "Weapon01";  
             break;
             case (1):
             spawnWep3.transform.position = new Vector3(spawnPoint2.transform.position.x, spawnPoint2.transform.position.y, spawnPoint2.transform.position.z);
             spawnWep3.transform.tag = "Weapon02";  
             break;
             case (2):
             spawnWep3.transform.position = new Vector3(spawnPoint3.transform.position.x, spawnPoint3.transform.position.y, spawnPoint3.transform.position.z);
             spawnWep3.transform.tag = "Weapon03";  
             break;
             case (3):
             spawnWep3.transform.position = new Vector3(spawnPoint4.transform.position.x, spawnPoint4.transform.position.y, spawnPoint4.transform.position.z);
             spawnWep3.transform.tag = "Weapon04";  
             break;
         }
             case (3):
             var spawnWep4 = Instantiate(wep04);
             switch (i)
         {            
             case (0):
             spawnWep4.transform.position = new Vector3(spawnPoint1.transform.position.x, spawnPoint1.transform.position.y, spawnPoint1.transform.position.z);
             spawnWep4.transform.tag = "Weapon01";  
             break;
             case (1):
             spawnWep4.transform.position = new Vector3(spawnPoint2.transform.position.x, spawnPoint2.transform.position.y, spawnPoint2.transform.position.z);
             spawnWep4.transform.tag = "Weapon02";  
             break;
             case (2):
             spawnWep4.transform.position = new Vector3(spawnPoint3.transform.position.x, spawnPoint3.transform.position.y, spawnPoint3.transform.position.z);
             spawnWep4.transform.tag = "Weapon03";  
             break;
             case (3):
             spawnWep4.transform.position = new Vector3(spawnPoint4.transform.position.x, spawnPoint4.transform.position.y, spawnPoint4.transform.position.z);
             spawnWep4.transform.tag = "Weapon04";  
             break;
         }
             break;
         }
 
     }
 
 }

What the hell am I doing wrong?

(The script is in JavaScript and please reply as though your explaining it to a 5 year old...lol)

Any help is much appreciated !

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 GameVortex · Jan 07, 2015 at 08:30 AM 1
Share

Have you Debugged out what random1 is and checked if the case ever gets to the wep01 and wep02 spawning? Have you double checked that the correct weapon prefabs are attached to the wep variables?

You code could also be simplified a lot by using arrays and more functions.

avatar image Digital-Phantom · Jan 07, 2015 at 09:02 AM 0
Share

could you give an example of the Debug line I'd need to use and where exactly to put it?

avatar image GameVortex · Jan 07, 2015 at 09:10 AM 1
Share

The Debug.Log functionality is your friend and you should learn to use it well. The function Debug.Log takes any object and prints out its name/type/value into the console of unity (found at the bottom of the unity editor or open it with Window/Console). So in your instance you can put a Debug.Log right after your random function to see what the random number is by doing this:

 Debug.Log("Random Number: " + random1);

Or after each instantiate line to know which is being used:

 Debug.Log("Instantiating Weapon Number: " + random1 + " At SpawnPoint: " + i);


2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Juice-Tin · Jan 07, 2015 at 08:29 AM

You should put Debug.Log("creating weapon 1"); etc in your code, to see exactly where the code is/isn't running.

Also your "switch(random1)" is missing breaks after each case. So if it was 0, it would then fall down to case 1 and get replaced, etc.

Try fixing that and putting in a lot of Debug.Log's to see exactly how your code is behaving. :)

Comment
Add comment · Show 1 · 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 Digital-Phantom · Jan 07, 2015 at 09:02 AM 0
Share

could you give an example of the Debug line I'd need to use and where exactly to put it?

avatar image
0

Answer by Digital-Phantom · Jan 07, 2015 at 10:15 AM

Thanks for the help guys but I think I'm trying to run before I can walk. I'm going to go back to an older (but simpler) script I was using. Although very basic I wrote it myself and can at least follow it and can expand it and learn at the same time. May come back to this script at a later date when I'm a little (or a lot) more competent...lol Again... many thanks for the help !

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

27 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

Spawn game object in random position on screen 1 Answer

Random Spawner using radius and numbers 3 Answers

Spawn random amount apart of each other 1 Answer

How to spawn objects in a specific range of random location 1 Answer

Simple array an spawning question 6 Answers


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