- Home /
Prefab Script List not being assigned as unique - can you see why?
First, thanks for any help here - I'm reluctant to ask because it's probably something obvious I'm missing but i've been looking at it too long...
The scenario is I have a holder object with a pathfinding script (working fine) and an event-based assignment script.
The objects being assigned are a group of instantiated prefab characters with a script added that navigates the path calculated from the pathfinding script when assigned from the assignment script.
If they all follow the same exact path, it's working great.
What I am trying to do is append a unique "final location" for each prefab. Think of a bridge crew running on to the bridge then breaking up to battle stations.
Here is the simplified assignment script:
public List<Vector3> RunPath; //populated by the pathfinding script public bool ExecutePath;//set by the pathfinding script once the entire path is calculated public GameObject[] CharacterArray; void Start () { %|-147361493_1|% %|-950286578_2|% } void Update () { %|1226425402_3|% %|737211570_4|% %|-481316925_5|% %|1764865785_6|% %|177424711_7|% %|882064304_8|% %|306964567_9|% %|-1581774797_10|% %|-958725657_11|% %|-1565622815_12|% %|-1720740372_13|% %|130943564_14|% %|-98425953_15|% }
The issue is every prefab has a public List Path; variable and every prefab is getting every vector3 added to Path from CharacterFinalPositions[intClassCount]) which is the List of unique final positions. In other words, each prefab should get just one but instead, each prefab is getting them all.
Thanks again for any advice.
public List<Vector3> ClassPath;//populated by the pathfinding script
public bool ExecutePath;//set by the pathfinding script once the entire path is calculated
public GameObject[] CharacterArray;
void Start ()
{
CharacterArray = GameObject.FindGameObjectsWithTag("Student");
ExecutePath = false;
}
void Update ()
{
if (ExecutePath == true)
{
int intClassCount = 0;
foreach (GameObject nextCharacter in CharacterArray)
{
nextCharacter.GetComponent<FollowPath>().Path = ClassPath;//list of vector3s
nextCharacter.GetComponent<FollowPath>().Path.Add(CharacterFinalPositions[intClassCount]);
//CharacterFinalPositions is a list of unique vector 3s
nextCharacter.GetComponent<FollowPath>().AtTarget = false;
intClassCount++;
}
}
ExecutePath = false;
}
Answer by LazyElephant · Dec 13, 2015 at 09:28 PM
nextCharacter.GetComponent<FollowPath>().Path = ClassPath;
This line doesn't create a unique copy of the list for each object, it just assigns a reference to the list you already have. Since all the objects are referencing the same list, all the endpoints get added to that single list and the objects all follow the same path.
You will have to make sure you are creating a new list for each object before you add the unique end point to it.
This will, of course, create a lot of lists with mostly the same elements and waste memory. As an alternative, you could give all your objects a reference to the base ClassPath list, but store the endpoints in a separate Vector3 variable for each object.
Your answer
Follow this Question
Related Questions
using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers
problem with a list that has to store different things for multiple gameobjects 1 Answer
How to not have a function affect a prefab? 2 Answers
Create with code 2.2 pizza not shooting 1 Answer
Prefab doesn't keep it's public variable when Instantiated? 2 Answers