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 $$anonymous$$ · Dec 10, 2017 at 03:31 AM · instantiatearraysynchronizationarray of gameobjects

Problem with object generation script?

Hello, I made a script to generate a certain amount of objects in random places around the game map, but after realizing that my code was disguising (it involved twenty if-statements rather than two foreach statements) I decided it was time to fix it, but I am not really too sure on how to sync the number of objects left to be generated with the type of object that needs to be generated. I tried to sync it by running a foreach-statement inside of a foreach-statement, but I don't think Unity likes that very much.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EcoGeneraterScript : MonoBehaviour {
 
     public GameObject Map;
     public GameObject[] GeneratedObjects;
     public int[] NumberofObjectsToGenerate;
 
     void Update () {
         foreach (int NumberofObjectToGenerate in NumberofObjectsToGenerate) {
             foreach (GameObject GeneratedGameObject in GeneratedObjects) {
                 if (NumberofObjectToGenerate > 0) {
                     Instantiate (GeneratedGameObject, new Vector3 (Random.Range (0, Map.transform.localScale.x), 0, Random.Range(0, Map.transform.localScale.y)), Quaternion.identity);
                     NumberofObjectToGenerate += -1;
                 }
             }
         }
     }
 }

Error that I am getting: Assets/Scripts/EcoGeneraterScript.cs(16,6): error CS1656: Cannot assign to NumberofObjectToGenerate' because it is a foreach iteration variable'

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
Best Answer

Answer by fdz_ · Dec 10, 2017 at 04:07 AM

Does this help? Using for instead of foreach.

 for ( int i = 0; i < NumberofObjectsToGenerate.Length; i++ ) {
     foreach ( GameObject GeneratedGameObject in GeneratedObjects ) {
         if ( NumberofObjectsToGenerate[ i ] > 0 ) {
             Instantiate( 
                 GeneratedGameObject,
                 new Vector3(
                     Random.Range( 0, Map.transform.localScale.x ),
                     0,
                     Random.Range( 0, Map.transform.localScale.y )
                 ),
                 Quaternion.identity
             );
             NumberofObjectsToGenerate[ i ] -= 1;
         }
     }
 }

Other thing. There's special reason why are you doing this on Update instead of Start?

Comment
Add comment · Show 3 · 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 $$anonymous$$ · Dec 10, 2017 at 04:24 AM 0
Share

I'm confused, what are all these i's? Also, there are two semicolons on line one, which is out of my comprehension. Would you $$anonymous$$d rephrasing to something a little more simple?

avatar image fdz_ $$anonymous$$ · Dec 10, 2017 at 05:20 AM 0
Share

Can you understand this? This is basically the same as a for loop.

 int i=0;
 while ( i < NumberofObjectsToGenerate.Length ) {
     foreach ( GameObject GeneratedGameObject in GeneratedObjects ) {
         if ( NumberofObjectsToGenerate[ i ] > 0 ) {
             Instantiate(
                 GeneratedGameObject,
                 new Vector3(
                     Random.Range( 0, $$anonymous$$ap.transform.localScale.x ),
                     0,
                     Random.Range( 0, $$anonymous$$ap.transform.localScale.y )
                 ),
                 Quaternion.identity
             );
             NumberofObjectsToGenerate[ i ] -= 1;
         }
     }
     i++;
 }


Checkout this for (C# Reference) and here's a video by VoidRealms.

But maybe you should look/read some tutorials about C# basics. Here are some youtube playlists:

VoidRealms - C#

Brackeys - How to program in C# - Beginner Course

quill18 - C# for Complete Beginners - Tutorial


I'll leave you with a comparison between the for and foreach loop, since they're very similar. $$anonymous$$aybe this will help you to understand it.

 for ( int i = 0; i < GeneratedObjects.Length; i++ ) {
     GameObject GeneratedGameObject = GeneratedObjects[i];
 }
 
 foreach ( GameObject GeneratedGameObject in GeneratedObjects ) {
 
 }


avatar image $$anonymous$$ fdz_ · Dec 10, 2017 at 02:22 PM 0
Share

I ended up using the first one after watching the unity video about loops.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class EcoGeneraterScript : $$anonymous$$onoBehaviour {
 
     public GameObject $$anonymous$$ap;
     public GameObject[] GeneratedObjects;
     public int[] NumberofObjectsToGenerate;
 
     void Update () {
         for ( int i = 0; i < NumberofObjectsToGenerate.Length; i++) {
             foreach ( GameObject GeneratedGameObject in GeneratedObjects ) {
                 if ( NumberofObjectsToGenerate[i] > 0 ) {
                     Instantiate(GeneratedGameObject, new Vector3(Random.Range( 0, $$anonymous$$ap.transform.localScale.x * 1000 ), 0, Random.Range(0, $$anonymous$$ap.transform.localScale.y * 1000 )),Quaternion.identity);
                     NumberofObjectsToGenerate[i] -= 1;
                 }
             }
         }
     }
 }

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

99 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

Related Questions

Issue with instantiating multiple clone GameObjects 1 Answer

how to set a GameObject variable to a GameObject[] variable ? - C# 1 Answer

Randomizing a selection from an array 1 Answer

how to use multiple spawn points 1 Answer

Accessing next Camera from array 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