- Home /
How to have unique instances of script on multiple instances of prefab?
I have a prefab that uses a script, blend shapes, etc. When one object gets a ray cast event, it triggers the script/blendshapes, etc. for all the instances.
How can I fix this?
Example,
public void SpawnPrefab()
{
Vector3 position = new Vector3(Random.Range(collider.bounds.min.x,collider.bounds.max.x),
Random.Range(collider.bounds.min.y,collider.bounds.max.y),
Random.Range(collider.bounds.min.z,collider.bounds.max.z));
GameObject theObject = (GameObject)Instantiate(prefab, position, Quaternion.identity);
}
You need to show us the context (i.e. how the Raycast is done that triggers the SpawnPrefab() function. There is not enough information here to do more than guess at an answer.
Different instances of prefabs do use unique script instances. The problem is in code you did not provide.
Answer by unityWoot · Jul 10, 2014 at 01:45 AM
Ahhh, fixed. :)
In one of the updates for the script, I just made sure the ray hit a rigid body, but wasn't making sure the rigid body was the current rigid body,
// portion of the update function
if (!hit.rigidbody)
{
return;
}
if (hit.rigidbody.transform.name != transform.name)
{
return;
}
// if we pass both if-tests, we play blend shapes, etc.
Thanks :D
Thanks for returning and posting your solution! It just answered an issue I've had all day.
Your answer
Follow this Question
Related Questions
Tagging object while instating not working. 3 Answers
Change assigned prefab from script 3 Answers
Get Component from Instantiated Prefab 1 Answer
How to access script variables attached to a prefab at runtime in Javascript? 2 Answers
Instantiating Prefab from Javascript - BCE0005: Unknown identifier: 'Prefab' 2 Answers