Why do two different prefabs with two different scripts take each other's variables?
So, bit of a long one, here.
I've been trying to learn Unity through doing, mostly. I have been building a little platformer, and trying to make it semi-randomized, by providing small-ish chunks of maps as prefabs, then organizing them into a line to create one long chain of them. The issue started with having more than one.
In order to randomize it a bit more, I'm feeding a List of positions into each prefab, first through the Inspector as the Lists were public, and now through code as that wasn't working. The problem is that, occasionally, one prefab will take coordinates from the other prefab's List, and vice versa. I'm still unfamiliar with Unity and C# in general, so I'm really not certain why this is happening.
Here's an example of one of the scripts I'm using:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Random = UnityEngine.Random;
public class BoardOne: MonoBehaviour {
private List <Vector2> enemyPositions = new List<Vector2> ();
private List <Vector2> coinPositions = new List<Vector2> ();
private List <Vector2> boxPositions = new List<Vector2> ();
private List <Vector2> grassPositions = new List<Vector2> ();
public GameObject[] enemy;
public GameObject coin;
public GameObject box;
public GameObject grass;
private int enemyMax = (Random.Range (1,3));
private int coinMax = (Random.Range (2,5));
private int grassMax = (Random.Range (0,2));
private Transform itemHolder;
// Use this for initialization
void Awake ()
{
enemyPositions.Add (new Vector2(4.8f, 4.55f));
enemyPositions.Add (new Vector2(14.4f, 2.95f));
enemyPositions.Add (new Vector2(12.8f, -0.25f));
enemyPositions.Add (new Vector2(6.4f, -0.25f));
coinPositions.Add (new Vector2(6.4f, 4.8f));
coinPositions.Add (new Vector2(14.4f, 3.2f));
coinPositions.Add (new Vector2(9.6f, 1.6f));
coinPositions.Add (new Vector2(6.4f, 1.6f));
coinPositions.Add (new Vector2(11.2f, 1.6f));
coinPositions.Add (new Vector2(12.8f, 3.2f));
boxPositions.Add (new Vector2(17.6f, -0.43f));
boxPositions.Add (new Vector2(3.2f, -0.43f));
boxPositions.Add (new Vector2(3.2f, 4.37f));
grassPositions.Add (new Vector2(6.4f, -1f));
grassPositions.Add (new Vector2(14.4f, -1f));
grassPositions.Add (new Vector2(4.8f, 3.8f));
ItemGenerate ();
}
void ItemGenerate()
{
itemHolder = new GameObject ("Item").transform;
for (int x = 0; x < enemyMax + 1; x++)
{
int randomIndex = Random.Range (0, enemyPositions.Count);
Vector2 randomPosition = enemyPositions[randomIndex];
enemyPositions.RemoveAt (randomIndex);
randomPosition.x += (GameManager.boardCount * 22.4f);
GameObject enemyChoice = enemy[Random.Range (0, enemy.Length)];
GameObject instance = Instantiate (enemyChoice, randomPosition, Quaternion.identity) as GameObject;
instance.transform.SetParent (itemHolder);
}
for (int x = 0; x < coinMax + 1; x++)
{
int randomIndex = Random.Range (0, coinPositions.Count);
Vector2 randomPosition = coinPositions[randomIndex];
coinPositions.RemoveAt (randomIndex);
randomPosition.x += (GameManager.boardCount * 22.4f);
GameObject instance = Instantiate (coin, randomPosition, Quaternion.identity) as GameObject;
instance.transform.SetParent (itemHolder);
}
for (int x = 0; x < grassMax + 1; x++)
{
int randomIndex = Random.Range (0, grassPositions.Count);
Vector2 randomPosition = grassPositions[randomIndex];
grassPositions.RemoveAt (randomIndex);
randomPosition.x += (GameManager.boardCount * 22.4f);
GameObject instance = Instantiate (grass, randomPosition, Quaternion.identity) as GameObject;
instance.transform.SetParent (itemHolder);
}
for (int x = 0; x < 1; x++)
{
int randomIndex = Random.Range (0, boxPositions.Count);
Vector2 randomPosition = boxPositions[randomIndex];
boxPositions.RemoveAt (randomIndex);
randomPosition.x += (GameManager.boardCount * 22.4f);
GameObject instance = Instantiate (box, randomPosition, Quaternion.identity) as GameObject;
instance.transform.SetParent (itemHolder);
}
}
// Update is called once per frame
void Update () {
}
}
Any thoughts? Or any suggestions in general? Still learning, here, and always happy to have help.
Your answer
Follow this Question
Related Questions
Locate _MainTex from a public shader and assign to instantiated prefab for GUI 1 Answer
GameObject positions not lining up 0 Answers
Spawn in a different and random Spawn point from a list 0 Answers
Script updates prefab text 0 Answers
2D Shooter Make Item Pickup Change Bullet Prefab On Collision 1 Answer