I have a lot of objects and they all use one the same script.
I want to change script’s value for only one object. How can I do it?
Doesn’t work:
Component obj;
void Start ()
{
obj = gameObject.GetComponent<WaypointAI>();
}
void OnTriggerEnter()
{
obj.patrolSpeed = 5;
}
Work, but change value for all objects, which use script, not for one:
MyScriptName obj;
void Start ()
{
obj = gameObject.GetComponent<ComponentWhichContainScript>();
}
void OnTriggerEnter()
{
obj.patrolSpeed = 5;
}
give tag to that gameobject when it collide and check the gameobject which has tag and you script and change value of that gameobject script
I’m not sure of what you call script and object (GameObject and Component?)
But if you have the same component on several gameobject you can add a public field to your component and then change the value of your object from the Inspector.
public int speed = 5;
//...
void OnTriggerEnter()
{
obj.patrolSpeed = speed;
}
Update :
Check if your special component exist/ is present and change the value if required.
bool isSpecial;
void Start ()
{
obj = gameObject.GetComponent<ComponentWhichContainScript>
isSpecial = (obj != null);
//or something like this isSpecial = obj.IsSpecial();
}
void OnTriggerEnter()
{
obj.patrolSpeed = (isSpecial)? 4 : 5;
}