- Home /
Initializing objects with another scripts variables.
I want to start by saying I have read this page 10 times, so please don't just link it. http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
I have two scripts, playerStats, and gameManagment. Within gameManagment script I first initialize the other script with awake and then attempt to initialize some standard "players", attach the playerStats script to them. I now want to edit each players stat individually, but am having trouble doing so.
The gameManagment script is assigned to an empty gameobject within unity. I wish to give each new "player" object the playerStats script within gameManagment.
GameManagment script:
public PlayerStats playerStats;
void Awake()
{
playerStats = GetComponent<PlayerStats>();
}
void Start()
{
//create players
GameObject player1 = GameObject.CreatePrimitive(PrimitiveType.Capsule);
player1.transform.position = new Vector3(4,1,0);
player1.AddComponent<PlayerStats>();
player1.tag = "Player";
//player1.PlayerStats.setSpeed(10);
//this line is my intended implementation, but will not compile
GameObject player2 = GameObject.CreatePrimitive(PrimitiveType.Capsule);
player2.transform.position = new Vector3(0,1,4);
player2.AddComponent<PlayerStats>();
player2.tag = "Player";
//playerStats.setSpeed(5);
//this line compiles, but gives me a null reference exception
//at runtime
}
PlayerStats script :
public class PlayerStats : MonoBehaviour {
public float speed = 0;
public float health = 100;
public bool dead = false;
void Start(){
speed = 0;
health = 100;
}
public void setSpeed(float amount)
{
speed = amount;
}
Answer by robertbu · May 06, 2014 at 11:01 PM
AddComponent() returns a reference to the component, so you could replace/insert at line 13:
PlayerStats ps1 = player1.AddComponent<PlayerStats>();
ps1.setSpeed(15.0f);
While this is the best solution, you could also use GetComponent() to solve the problem. So anytime after you've added PlayerStats, you could do:
PlayerStats ps1 = player1.GetComponent<PlayerStats>();
or if you are sure the component is there, you can create a compound statement:
player1.GetComponent<PlayerStats>().setSpeed(11.0f);
I don't know if it will help you, but here is another reference page for GetComponent():
Thanks Robert! I have been puzzling over this all day and that worked great.
Are there any advantages to the compound statement?
Compound statements make the code look a bit cleaner. They have two downsides. First, if there is any chance the 'GetComponet()' can fail (i.e. the component that you are seeking does not exist on the game object), then a compound statement will cause a null reference exception. The second issue is performance. While not a huge deal, GetComponent() does have some cost. If you are going to be referencing a component every frame, then you would want to declare a variable at the top of the class, then initialize that variable in Start(). That way you could use do ps1.setSpeed() or whatever each frame without calling GetComponent().
Your answer
Follow this Question
Related Questions
Assigning a gameObject in the script 3 Answers
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
Problem with taking a gameobject from gameobject list to inventory. 1 Answer
Glow single Object 1 Answer