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 Yezen0Nizam · Feb 08, 2020 at 09:59 AM · instantiatelistrandomlists

How to stop instantiating a certain object / objects from a list .

My code instantiates a random ball but I want to exclude a ball/balls in the run time on certain conditions ,How to do that ?

  public Ball[] balls;
   Ball randomBall = balls[Random.Range(0, balls.Length)];
  public void InstantiateBall()
      {
          Ball randomBall = balls[Random.Range(0, balls.Length)];
          ballCountInScene = FindObjectsOfType<Ball>().Length;
          if (ballCountInScene == 0 )
          {
              ballDestroyed = true;
              Instantiate(randomBall , spawnPoint.position, Quaternion.identity);
          }
          else
          {
              ballDestroyed = false;
          }
      }
Comment
Add comment · Show 1
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 rh_galaxy · Feb 08, 2020 at 03:13 PM 0
Share

When do you call InstantiateBall()? Also, is it intentional that you declare "Ball randomBall" twice? How do you populate balls[]?, in the inspector?

Seems this method will create another ball duplicate from the array once if there are no balls in the scene, and that includes the balls in balls[] also I think?

I think you have to think this through, what is it that you want to do?

3 Replies

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

Answer by Hellium · Feb 08, 2020 at 06:43 PM

Following code not tested

 private List<Func<GameObject,bool>> conditions = new List<Func<GameObject,bool>>();
 public Ball[] balls;    
 
 public void InstantiateBall()
 {
     List<GameObject> spawnableBalls = GetSpawnableBalls();
     
     if(spawnableBalls.Count == 0)
         return ;
         
     Ball randomBall = spawnableBalls[Random.Range(0, spawnableBalls.Count)];
     ballCountInScene = FindObjectsOfType<Ball>().Length;
     if (ballCountInScene == 0 )
     {
         ballDestroyed = true;
         Instantiate(randomBall , spawnPoint.position, Quaternion.identity);
     }
     else
     {
         ballDestroyed = false;
     }
 }
 
 private List<GameObject> GetSpawnableBalls()
 {
     List<GameObject> spawnableBalls = new List<GameObject>();
 
     for( int i = 0 ; i < balls.Length ; ++i)
     {
         if(IsSpawnable(balls[i]))
             spawnableBalls.Add( balls[i] );
     }
     return spawnableBalls;
 }
 
 private bool IsSpawnable(GameObject ball)
 {
     for( int i = 0 ; i < conditions.Count ; ++i)
     {
         if(conditions[i](ball))
             return false;
     }
 
     return true;
 }

 // Example to stop spawnling blue balls
 public void StopSpawningBlueBalls()
 {
     conditions.Add(new Func<GameObject,bool>( ball => ball.GetComponent<Renderer>().color == Color.blue ));
 }
 
 // Example to stop spawnling big balls
 public void StopSpawningBigBalls()
 {
     conditions.Add(new Func<GameObject,bool>( ball => ball.transform.lossyScale.sqrMagnitude > 100 ));
 }

 // Implement the other functions you need in order to exclude the balls under certain conditions
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
avatar image
1

Answer by tormentoarmagedoom · Feb 08, 2020 at 12:51 PM

hey there.

you can just add a if line before instantiate to decide if stop the function.

 if(something) return;

if return is executed, InstantiateBall() function will be stoped fpr that iteration and code "jumps" to next part of the code.

Is this what you wanted?

Byee

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 Yezen0Nizam · Feb 08, 2020 at 02:29 PM 0
Share

I don't want to stop the function , I want to change the variables in the list , like let's say I have a blue , red and a yellow ball and I want to stop instantiating the blue ones in a certain condition . thanks for replying .

avatar image
1

Answer by lvjuvan · Feb 08, 2020 at 05:00 PM

Have subclasses for each type of ball, e.i. classes inheriting from the Ball class and then use the typeof operator to determine if you want to instantiate.,Not sure if I understand the question, but one way of doing it would be to have subclassess of ball, e.g. blue ball, red ball and so on, all inheriting from ball and use the typeof operator to check if it should be instantiated or if it should try again.

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 lvjuvan · Feb 08, 2020 at 05:02 PM 0
Share

Or, have several arrays with the different types of balls, and compile the ones that you want to allow to a list and then take a random entry from that list.

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

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

Related Questions

Instantiate random from a list gives error: "Reference not set to an instance" 1 Answer

Random select from array and spawn 1 Answer

How do I Photon Instantiate a scene object randomly from a list of objects? 0 Answers

Random instantiation endlessly? 1 Answer

Return question’s answer at the determined index 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