- Home /
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'
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?
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?
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:
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 ) {
}
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;
}
}
}
}
}