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 ShinyTaco · Jan 08, 2016 at 07:36 AM · gameobjectinstantiatearray

How to Instantiate a Different Game Object After Getting to the End of an Array?

I have an array. It randomly chooses a number, let's say from 1 to 20 for example.

A game object is instantiated every time a button is pushed. Fine no problem.

Let's say the array has 20 elements for example. After I push a button, I need a different game object to instantiate.

The problem is the new game object is instantiating when the last game object of the array is instantiating.

Basically, the last game object in the array and the different game object are instantiating at the same time.

What needs to happen is when the user pushes the button, the last game object of the array is instantiated.

Then, the next time the user pushes the button the different game object needs to be instantiated. Not at the same time as the last element in the array.

I've tried bools and other ridiculous ideas that didn't work.

I know it's something very simple, but have no idea.

Any ideas how to fix this problem?

Thanks!

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 ZefanS · Jan 08, 2016 at 07:50 AM 1
Share

Please post your code or it's hard to help.

avatar image ShinyTaco ZefanS · Jan 08, 2016 at 08:21 AM 0
Share

There's not much to show really. When a button is pushed, let's say the spacebar, I instantiate one prefab at a time from spawnStuff().

When i++ is at the end of the array eg all the array's elements have been spawned, spawnOneDifferentObjectEndOfArray() is called when the last element of the array is spawned.

spawnOneDifferentObjectAtEndOfArray() needs to be called on the Next push of the spacebar, not when the last element of the array is spawned.

  void spawnStuff(){
      // Spawn Stuff from Array Here
     spawnOneDifferentObjectAtEndOfArray();
 }
 
 
  void spawnOneDifferentObjectAtEndOfArray(){
         // Instantiate Here
 
         // The problem is here
         // This method is getting called at the same time as the last game object        // is instantiated 
     }
         
avatar image ZefanS ShinyTaco · Jan 08, 2016 at 08:29 AM 0
Share

Are you detecting when you reach the end of the array? $$anonymous$$aybe something along these lines would work?

 if the space bar is pressed:
     if the array isn't empty:
         spawn stuff;
     else:
         spawn that thing at the end;

And then in SpawnStuff() you need to check for the array being empty:

 spawn something from the array;
 
 if that was the last thing in the array:
     the array is empty;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by saschandroid · Jan 08, 2016 at 11:34 AM

 int m_index = 0;
 void Update()
 {
     if (Input.GetKeyDown(KeyCode.Space))
     {
         if ( m_index < firstArray.Length)
         {
              // spawn firstArray[m_index]
             m_index++;
         }
        else
        {
              // spawn otherObject
         }
     }
 }

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

41 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

Related Questions

Save Gameobject in Array 1 Answer

Want replace a current object with one inside array the object being replaced with is prefab 1 Answer

Instantiate from array into array? 2 Answers

Instantiating a gameObject in an array is not working for me 1 Answer

Referencing an instantiated object with a global variable 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