Change speed of character using at runtime?

I have been trying to figure this out for quite some time including 2 more questions on the forums with no answers. So I’m going to go into detail on this and hopefully someone might be able to point me in the right direciton. Here is my situation:

I have a parent with 2 childed objects, which I will refer to as player1 and player2. The parent has a character controller attached to it. The game starts off with player1 active and player2 inactive. When the character controller (parent) collects a powerup and enters a trigger, player1 becomes inactive and player2 becomes active. When player 2 becomes active I want the player’s speed to increase. I have the power up script attached to the Power Up game object. I am also including the movement script attached to the Parent. Please note this is not all of my script… but it should be enough to understand my problem.

Power Up Script:

private var player1 : GameObject;

private var player2: GameObject;

private var powerup: GameObject;

function Awake () {

player1 = GameObject.FindWithTag(“Player”);

player2 = GameObject.FindWithTag(“Player2”);

powerup = GameObject.FindWithTag(“Powerup”);



function OnTriggerEnter(other : Collider){





Movement Script (attached to Parent Object):

var walkSpeed = 15;

var runSpeed = 40;

private var player1 : GameObject;

private var player2: GameObject;

function UpdateSmoothedMovementDirection () {

player1 = GameObject.FindWithTag(“Player”);

player2 = GameObject.FindWithTag(“Player2”);

var h = Input.GetAxisRaw ("Horizontal");

if (!canControl)

	h = 0.0;

movement.isMoving = Mathf.Abs (h) > 0.1;
if (movement.isMoving)

	movement.direction = Vector3 (h, 0, 0);

if (controller.isGrounded) {

	// Smooth the speed based on the current target direction

	var curSmooth = movement.speedSmoothing * Time.deltaTime;
	// Change Character Speed

	var targetSpeed = Mathf.Min (Mathf.Abs(h), 1.0);

		targetSpeed *= movement.walkSpeed;
	var newSpeed = Mathf.Min (Mathf.Abs(h), 1.0);

		newSpeed *= movement.runSpeed;
		if ( 
	movement.speed = Mathf.Lerp (movement.speed, targetSpeed, curSmooth);

	if ( 
	movement.speed = Mathf.Lerp (movement.speed, newSpeed, curSmooth);

So when I run the game there is a “NullReferenceException” on the second to last line of the player movement script: "if ( i’m assuming this because the engine assumes player 2 should be active when the game starts and he is not. What i’m trying to tell the engine to do is acknowledge when player2 becomes active that the newSpeed should be used instead of the targetSpeed. If I remove this line of code the game starts and you can control player 1 at normal speed. However once I collect the power up you cannot control the the new character because I removed the line that made it possible to control him (if ( I hope someone understands what i’m trying to do here and can help me solve this. Thank you for your time.

That’s because all the Find functions only search for active objects. As only one of your players is active, you will always have an error. To fix that, you should initiate player1 and 2 from the movement script before one of them get unactivated, or use another way to know what speed should be use. Maybe get access to the movement script from the power up, and change some boolean isPlayer1 etc.