Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Daphoeno · Aug 27, 2014 at 01:20 PM · javascriptgameobjectfindworking

"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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daphoeno · Aug 27, 2014 at 01:32 PM 0
Share

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.

avatar image Daphoeno · Aug 27, 2014 at 01:35 PM 0
Share

After doing that, it did seem to fix it! Thank you very much!

avatar image BlackHoleStorm · Aug 27, 2014 at 01:51 PM 2
Share

No problem, happy to help

avatar image
1

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 
 
 }
  

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Daphoeno · Aug 27, 2014 at 01:32 PM 0
Share

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.

avatar image Daphoeno · Aug 27, 2014 at 01:35 PM 0
Share

After doing that, it did seem to fix it! Thank you very much!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

24 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Finding Assets/Prefabs That Are Not In The Scene 1 Answer

Find the first game object created with a given name 1 Answer

Find Transform in the scene 2 Answers

Separate objects from GameObject[] group 0 Answers

Find Keyword in a Searched GameObject (JS) 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges