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 eperezki · Jul 12, 2012 at 10:03 AM · spawnrandom.range

Random.Range Spawner

I'm trying to make 1 of 3 monster to spawn randomly but this code spawns all of them at the same time.

 var valo : GameObject;
     var monster1 : GameObject;
     var monster2 : GameObject;
     var monster3 : GameObject;        
     
     
     function Start(){
      Time.timeScale = 1;
     
      yield WaitForSeconds(1);
      
      particleEmitter.emit = false;
      
      valo.active = false;
      
      yield WaitForSeconds(1);
      
      var chance =(Random.Range(1, 4));
      
      if(chance == 1){
      monster1.SetActiveRecursively(true);
      }
          else if(chance == 2){
      monster2.SetActiveRecursively(true);
      }
          else if(chance == 3){
      monster3.SetActiveRecursively(true);
      }
          
          
      }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by whydoidoit · Jul 12, 2012 at 10:06 AM

That code isn't spawning anything (it is not doing an Instantiate) - I presume your 3 monsters are already in the scene (if they are all appearing then I would imagine that they are all active and that this code just sets them active again - which won't do much).

You could try this in addition:

 function Awake() {
      monster1.SetActiveRecursively(false);
      monster2.SetActiveRecursively(false);
      monster3.SetActiveRecursively(false);
 }

Which would ensure that they were all deactivated.

EDIT The problem was that the script was attached to more than one object.

Comment
Add comment · Show 8 · 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 eperezki · Jul 12, 2012 at 10:19 AM 0
Share

Yes they are already in the scene but not active. I have tried Debug.Log(chance) and it shows like "1 2 2 3 1 2 1 2 3 3 2 2 1..." and it probably makes all the monsters active? But I want it to stop at "1" or "2" or "3" so only one could spawn.

avatar image whydoidoit · Jul 12, 2012 at 10:21 AM 0
Share

What is this script attached to?

avatar image eperezki · Jul 12, 2012 at 10:23 AM 0
Share

Campfire in the middle of the map

avatar image whydoidoit · Jul 12, 2012 at 10:24 AM 0
Share

How is this being called multiple times is what I don't understand - if it's just attached to that one object it should only be called once - are you sure you don't have it attached to the monsters too or something?

avatar image eperezki · Jul 12, 2012 at 10:29 AM 0
Share

Oh, It is attached to Campfires Smoke, InnerCore and OuterCore. I'll try removing it from 2 of these.

Show more comments

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

(C#) Help with spawning a random GameObject 1 Answer

How do i prevent an object instantiating another object straight away 2 Answers

Not repeating random 2 Answers

Using Vector3 in ViewportToWOrldPoint 1 Answer

How To Random Range a Float? 2 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