- Home /
GUI Live Counter - Accessing another script
So I'm currently trying to have a GUI element access information from a different script, and then paste that onto the screen.
Script A (Does not include GUI Text, only the data the GUI tries to access)
using UnityEngine;
using System.Collections;
public class PlayerMovementScript : MonoBehaviour {
public float Speed = 2.0f;
public int Lives = 3;
public GameObject Player;
public int NumberOfShipsAlive = 1;
void FixedUpdate()
{
if (Input.GetKey(KeyCode.D))
{
transform.position += transform.forward * Speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.A))
{
transform.position -= transform.forward * Speed * Time.deltaTime;
}
if (Input.GetKey(KeyCode.R))
{
if (NumberOfShipsAlive == 0)
{
GameObject spawnPlayer = (GameObject)Instantiate (Player);
NumberOfShipsAlive += 1;
}
else
{
return;
}
}
}
void OnTriggerEnter()
{
Destroy(this.gameObject);
Lives -= 1;
NumberOfShipsAlive -= 1;
}
}
Script B (Includes GUI) using UnityEngine; using System.Collections;
public class GUIScript : MonoBehaviour {
public GameObject numberOfLives;
public GUIText LiveCounter;
// Use this for initialization
void Start ()
{
GameObject numberOfLives = GameObject.Find ("numberOfLives");
PlayerMovementScript playerMovementScript = numberOfLives.GetComponent<PlayerMovementScript>();
LiveCounter.text = numberOfLives.ToString ();
}
}
I just realized I'm getting a Null Reference Error on my
Player$$anonymous$$ovementScript player$$anonymous$$ovementScript = numberOfLives.GetComponent();
line in Script B. Looking into it now.
Answer by hvilela · Oct 19, 2012 at 11:30 PM
If you have an error at this line, your numberOfLives is null, meaning that GameObject.Find couldn't find an object named numberOfLives. Check for misspellings.
Oh yeah, sorry. Noticed right after I posted, but when I went back I couldn't see it for some reason.
Your answer
Follow this Question
Related Questions
Impact Force Gui 0 Answers
how can i get GUI.Button presse(or down not click) 3 Answers
GUI draw rectangle script not drawing rectangle! 1 Answer
Intro GUI Text Script... 3 Answers
GUI Resolution Ajust 1 Answer