Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 zbothoang · Oct 11, 2017 at 02:14 PM · unity 2d

How to spawn random objects with differents vector 3?

[SerializeField] private GameObject SpikeHolder; [SerializeField] private GameObject Missile; [SerializeField] private GameObject MovingSpikeUp; [SerializeField] private GameObject MovingSpikeDown;

 int randomObject;

 void Start()
 {
     InvokeRepeating("Spawner", 4f, 1f);
     StartCoroutine(Spawner());
 }

 IEnumerator Spawner()
 {
     yield return new WaitForSeconds(1);
     Vector3 temp = SpikeHolder.transform.position;
     temp.y = Random.Range(-2.5f, 2.5f);
     Vector3 temp2 = Missile.transform.position;
     temp2.y = Random.Range(-3.5f, 3.5f);
     Vector3 temp3 = MovingSpikeUp.transform.position;
     Vector3 temp4 = MovingSpikeDown.transform.position;
     

     randomObject = Random.Range(1, 4);
     if(randomObject==1)
     {
         Instantiate(SpikeHolder, temp, Quaternion.identity);
         InvokeRepeating("Spawner", 4f, 1f);
         StartCoroutine(Spawner());
     }
     else if(randomObject==2)
     {
         Instantiate(Missile, temp2, Quaternion.identity);
         InvokeRepeating("Spawner", 4f, 1f);
         StartCoroutine(Spawner());
     }
     else if(randomObject==3)
     {
         Instantiate(MovingSpikeUp, temp3, Quaternion.identity);
         InvokeRepeating("Spawner", 4f, 1f);
         StartCoroutine(Spawner());
     }
     else
     {
         Instantiate(MovingSpikeDown, temp4, Quaternion.identity);
         InvokeRepeating("Spawner", 4f, 1f);
         StartCoroutine(Spawner());
     }

My code have a problem, anyone can tell me how i can spawn random objects with different vector3????

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

Answer by Shippety · Oct 11, 2017 at 06:51 PM

Think your problem here is probably in the if / else statements. Random.Range() returns a float between the two numbers you set (https://docs.unity3d.com/ScriptReference/Random.Range.html) so only the default on your if / else statements will be true. What you could do is Mathf.RoundToInt() your randoms (https://docs.unity3d.com/ScriptReference/Mathf.RoundToInt.html) so that they're integers. Could look something like this:

 int randomObject;
  void Start()
  {
      InvokeRepeating("Spawner", 4f, 1f);
      StartCoroutine(Spawner());
  }
  IEnumerator Spawner()
  {
      yield return new WaitForSeconds(1);
      Vector3 temp = SpikeHolder.transform.position;
      temp.y = Random.Range(-2.5f, 2.5f);
      Vector3 temp2 = Missile.transform.position;
      temp2.y = Random.Range(-3.5f, 3.5f);
      Vector3 temp3 = MovingSpikeUp.transform.position;
      Vector3 temp4 = MovingSpikeDown.transform.position;

      // THIS PART!! 
      randomObject = Mathf.RoundToInt(Random.Range(1, 4));
 
      if(randomObject==1)
      {
          Instantiate(SpikeHolder, temp, Quaternion.identity);
          InvokeRepeating("Spawner", 4f, 1f);
          StartCoroutine(Spawner());
      }
      else if(randomObject==2)
      {
          Instantiate(Missile, temp2, Quaternion.identity);
          InvokeRepeating("Spawner", 4f, 1f);
          StartCoroutine(Spawner());
      }
      else if(randomObject==3)
      {
          Instantiate(MovingSpikeUp, temp3, Quaternion.identity);
          InvokeRepeating("Spawner", 4f, 1f);
          StartCoroutine(Spawner());
      }
      else
      {
          Instantiate(MovingSpikeDown, temp4, Quaternion.identity);
          InvokeRepeating("Spawner", 4f, 1f);
          StartCoroutine(Spawner());
      }

Although I didn't try that code out myself, that's the idea of what you wanna do. Not totally sure what you want to happen with the temp vectors, if you want them to be whole numbers you could do the same thing on each axis.

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 gnp89 · Oct 11, 2017 at 07:06 PM 0
Share

Random.Range(int m, int n) returns an int from 'm' to 'n-1' so that line should be good.

avatar image zbothoang · Oct 14, 2017 at 04:40 AM 0
Share

about the vector, i want each game object have their own vector, i saw so many excercise on web but all of them are spawn objects with same vector.

avatar image
0

Answer by gnp89 · Oct 11, 2017 at 07:04 PM

You're calling InvokeRepeating multiple times, starting the coroutine multiple times, quite unpredictable :)

Just calling InvokeRepeating solves your problem of spawning every X seconds.

You don't specify exactly what your problem is. Are you having an exception? Nothing happens? Is the first object instantiated but then it stops?

The following code should work if all your variables are assigned. You can make it more elegant by using a switch statement, and using arrays for holding the prefabs and their default positions.

 int randomObject;
  void Start()
  {
      InvokeRepeating("Spawner", 4f, 1f);
  }
  void Spawner()
  {
      Vector3 temp = SpikeHolder.transform.position;
      temp.y = Random.Range(-2.5f, 2.5f);
      Vector3 temp2 = Missile.transform.position;
      temp2.y = Random.Range(-3.5f, 3.5f);
      Vector3 temp3 = MovingSpikeUp.transform.position;
      Vector3 temp4 = MovingSpikeDown.transform.position;
      
      randomObject = Random.Range(1, 4);
      if(randomObject==1)
      {
          Instantiate(SpikeHolder, temp, Quaternion.identity);
      }
      else if(randomObject==2)
      {
          Instantiate(Missile, temp2, Quaternion.identity);
      }
      else if(randomObject==3)
      {
          Instantiate(MovingSpikeUp, temp3, Quaternion.identity);
      }
      else
      {
          Instantiate(MovingSpikeDown, temp4, Quaternion.identity);
      }

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 zbothoang · Oct 14, 2017 at 04:38 AM 0
Share

at the first time all of my objects were spawn and them it stop. I don't know what happen with my code

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

72 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 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 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

Finding relative mouse position 2 Answers

Do I need to destroy objects I'm not using? 4 Answers

Seeing different amounts of the game in different resolutions 0 Answers

how to add 2d cutscenes to unity 2 Answers

Possible to change size of cursor with Cursor.SetCursor? 0 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