- Home /
"GameObject.Find();" not working
I am currently making a version of asteroids within unity to test myself with coding within unity and also so that I can try and actually complete a project.
The "Asteroid" script (below) is giving me errors on running the game because the script doesn't seem to be able to find the gameobject named "Player" within my scene when "GameObject.Find();" is used.
#pragma strict
enum Size {Small, Medium, Large}
var size : Size;
var health : int;
var speed : float;
var Player : GameObject;
var explosion : Rigidbody;
private var player : Player;
player = Player.GetComponent("Player");
private var asteroidmedno : int = 0;
var asteroidmed1 : Rigidbody;
var asteroidmed2 : Rigidbody;
var asteroidmed3 : Rigidbody;
function Start ()
{
Player = GameObject.Find("Player");
if(size == Size.Small){
health = 10;
speed = Random.Range(40,50);
}else if(size == Size.Medium){
health = 20;
speed = Random.Range(30,40);
}else if(size == Size.Large){
health = 40;
speed = Random.Range(20,30);
}
if(size == size.Small){
rigidbody.AddForce (Vector3.forward * speed);
rigidbody.AddForce (Vector3.left * speed);
}else if(size == size.Medium){
rigidbody.AddForce (Vector3.back * speed);
rigidbody.AddForce (Vector3.left * speed);
}else if(size == size.Large){
rigidbody.AddForce (Vector3.forward * speed);
rigidbody.AddForce (Vector3.right * speed);
}
}
function Update ()
{
//speed += 0.1 * Time.deltaTime;
if(health == 0){
var clone : Rigidbody;
clone = Instantiate(explosion, transform.position, transform.rotation);
if(size == size.Large){
var med : Rigidbody;
asteroidmedno = Random.Range(1,3);
if(asteroidmedno == 1){
med = Instantiate(asteroidmed1, transform.position, transform.rotation);
med.rigidbody.AddForce(Vector3.forward * (speed * 2));
}else if(asteroidmedno == 2){
med = Instantiate(asteroidmed2, transform.position, transform.rotation);
med.rigidbody.AddForce(Vector3.forward * (speed * 2));
}else if(asteroidmedno == 3){
med = Instantiate(asteroidmed3, transform.position, transform.rotation);
med.rigidbody.AddForce(Vector3.forward * (speed * 2));
}
asteroidmedno = Random.Range(1,3);
if(asteroidmedno == 1){
med = Instantiate(asteroidmed1, transform.position, transform.rotation);
}else if(asteroidmedno == 2){
med = Instantiate(asteroidmed2, transform.position, transform.rotation);
}else if(asteroidmedno == 3){
med = Instantiate(asteroidmed3, transform.position, transform.rotation);
}
}
audio.Play();
Destroy (gameObject);
}
}
function OnTriggerEnter (other : Collider)
{
if(other.tag == "Bullet" && health > 0){
Destroy (other.gameObject);
Player.audio.Play();
player.Score = player.Score + 10;
health = health - 10;
}
//if(size == size.Large){
if(other.tag == "Ubound"){
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - 12);
}else if(other.tag == "Bbound"){
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 12);
}else if(other.tag == "Lbound"){
transform.position = new Vector3(transform.position.x + 20.5, transform.position.y, transform.position.z);
}else if(other.tag == "Rbound"){
transform.position = new Vector3(transform.position.x - 20.5, transform.position.y, transform.position.z);
}
//}
/*if(size == size.Medium){
if(other.tag == "Ubound"){
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z - 6.5);
}else if(other.tag == "Bbound"){
transform.position = new Vector3(transform.position.x, transform.position.y, transform.position.z + 6.5);
}else if(other.tag == "Lbound"){
transform.position = new Vector3(transform.position.x + 10, transform.position.y, transform.position.z);
}else if(other.tag == "Rbound"){
transform.position = new Vector3(transform.position.x - 10, transform.position.y, transform.position.z);
}
}*/
}
On running the game, I get this folowing error : "UnassignedReferenceException: The variable Player of Asteroid has not been assigned. You probably need to assign the Player variable of the Asteroid script in the inspector." even though the script itself is supposed to find and assign the gameobject when the game starts.
Any help is appreciated.
If you have any question please feel free to ask.
Thank you in advance.
Answer by BlackHoleStorm · Aug 27, 2014 at 01:23 PM
Line 14 has this
player = Player.GetComponent("Player");
Try moving that into the Start function.
That part references a script that is on the player and that seems to work fine where it is (I will move it anyway). "GameObject.Find();" is located at line 23 and it is in the start function.
After doing that, it did seem to fix it! Thank you very much!
Answer by Landern · Aug 27, 2014 at 01:26 PM
Well your assigning where you're declaring fields:
var Player : GameObject;
var explosion : Rigidbody;
private var player : Player;
player = Player.GetComponent("Player");
"player" shouldn't be set there, it should be set in start.
function Start ()
{
Player = GameObject.Find("Player");
player = Player.GetComponent.<Player>();
//... blah blah
}
That part references a script that is on the player and that seems to work fine where it is (I will move it anyway). "GameObject.Find();" is located at line 23 and it is in the start function.
After doing that, it did seem to fix it! Thank you very much!