changing speed of an instanced object

i have characters then are spawned random and walking with a speed of 1.

i have a script that says when it collides change the animation to run and i want the speed to change to 3.

i can successfully change the speed of the initial script but the problem is that it changes the speed of ALL the other walking models that share the script.

in my situation i cant make a script for each one because there may be multiple versions of the character in the scene at a time.

how can i make it just change the speed of the 1 character?

it would help if you post your script

heres my script to move my character

static var speed = 0.5;
var rotateSpeed : float = 3.0;

function Update () {
var controller : CharacterController = GetComponent(CharacterController);

// Rotate around y - axis
transform.Rotate(0, Input.GetAxis ("Horizontal") * rotateSpeed, 0);

// Move forward / backward
var forward : Vector3 = transform.TransformDirection(Vector3.forward);
var curSpeed : float = speed * Input.GetAxis ("Vertical");
controller.SimpleMove(forward * speed);
	
	if (score.anim ==0)
	{
	animation.Play("walk");
	}
	if (score.anim ==1)
	{
	speed = 2;
	animation.Play("run1");
	
	}
	

var dir  = Time.deltaTime;
var hit : RaycastHit;
if (Physics.Raycast(transform.position,transform.forward,hit,10))
	{
	if (hit.collider.gameObject.tag =="person")
		{
			transform.Translate(Time.deltaTime,0,0);

	}

}
}

heres my code for the collision

static var score = 0;
var poop : Transform;
static var anim = 0;

function OnCollisionEnter(collision : Collision){

var controller : CharacterController = GetComponent(CharacterController);
var forward : Vector3 = transform.TransformDirection(Vector3.forward);

if(collision.gameObject.tag =="poop")
{

anim=1;

}


	if (anim ==0)
	{
	animation.Play("walk");
	}
	if (anim==1)
	{
	
	//person_Move.speed = 1;

	animation.Play("run1");
	}


}

like i said it works perfectly besides the fact that it changes the speed of ALL characters with the script attached

I think that you are accidentally accessing all of the scripts when the collision happens, are these scripts attached to all of the characters (I know the one that moves the character is)

I don’t see the code where you’re doing your instantiation, but you probably want to do something like this:

var instantiatedCharacter = Instantiate(characterPrefab, transform.position, transform.rotation);
instantiatedCharacter.GetComponent(CharacterScript).speed = 10.0;

Obviously I’m making up script names and stuff, but you should get the idea.

When a variable is “static”, that means only one copy exists for every instance of that object.

Your speed variable is static, so every character uses the same speed, and if you change it, they don’t create their own copy. Just remove static and you should be fine. :slight_smile:

You can then set speed using

character.GetComponent( Person_Move ).speed = whatever;

thanks guys… took a tiny bit of revision but i got it working