user2966992
user2966992

Reputation: 13

Change value of Transform variable in Unity

I have a camera which follows a car when a player is driving. The issue is the car : transform variable in the UnityScript looks like this

var car : Transform;

Which means I would have to drag the transform onto the little box in the side panel to assign it.

Is it possible to assign this variable within the code like:

var car : Transform = Player1;

//BTW Player1 is the transform I want

The reason why it needs to be changed is in the code I want it to change between Player1 Player2 Player3

dependant on the currently selected one (i already coded this part)

Upvotes: 0

Views: 905

Answers (1)

S.Richmond
S.Richmond

Reputation: 11558

It depends on how and when you want to do it. From your Camera class you can do something like:

var car : Transform;

public void ChangePlayer(string playerName) {
  GameObject playerGO = GameObject.Find(playerName);

  if(playerGO != null)
    car = playerGO.transform;
}

Upvotes: 1

Related Questions