Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by Numisi · Aug 19, 2016 at 02:46 PM · scripting problemscripting beginnerscriptingbasics

GameObject.Find : why not working?

Hello everyone, maybe it will look like a stupid question to all of you, but until now I only programmed in darkbasic and I'm just starting with Unity, so please, make me understand why this code doesn't work. I want to have access to an object (child so it moves together) so I can use it's collision as a trigger. In the code it only rotates with Z because I was trying to see when the access worked, but it didn't, it continued saying ""NullReferenceException: Object reference not set to an instance of an object playerBehaviour.Update () (at Assets/Scripts/playerBehaviour.js:44)" I read and re-read the pages on the other object access and the GameObject.Find(), but I couldn't understand what I did wrong, neither figure it from the message. Please help, I'm used to a syntax much simpler, so it may look easy but it isn't for me. Code not working:

 #pragma strict
 
 public var giocatore : Rigidbody;
 private var velocita : float = 1;
 private var collinf : GameObject;
 
 function Start () {
 
 giocatore = GetComponent.<Rigidbody>();
 collinf = GameObject.Find ("Giocatore/CollisoreInferiore") ;
 
 }
 
 function Update () {
  if ( Input.GetKey (KeyCode.LeftShift) ) {
   velocita = 2;
  }
  else {
   velocita = 1;
  }
  if (Input.GetKeyDown (KeyCode.Space) ) {
   giocatore.velocity = Vector3 (0, 5, 0);
  }
  if ( Input.GetKey (KeyCode.W) ) {
   giocatore.transform.Translate (0, 0, velocita*Time.deltaTime);
  }
  if ( Input.GetKey (KeyCode.S) ) {
   giocatore.transform.Translate (0, 0, -velocita*Time.deltaTime);
  }
  if ( Input.GetKey (KeyCode.D) ) {
   giocatore.transform.Translate (Time.deltaTime, 0, 0);
  }
  if ( Input.GetKey (KeyCode.A) ) {
   giocatore.transform.Translate (-Time.deltaTime, 0, 0);
  }
  if ( Input.GetAxis ("Mouse X") > 0 ) {
  giocatore.transform.Rotate (0, 180*Time.deltaTime, 0);
  }
  if ( Input.GetAxis ("Mouse X") < 0 ) {
  giocatore.transform.Rotate (0, -180*Time.deltaTime, 0);
  }
  
  if ( Input.GetKey (KeyCode.Z) ) {
   collinf.transform.Rotate (0, 0, 180*Time.deltaTime);
  }
  
 }
Comment
Add comment · Show 3
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 meat5000 ♦ · Aug 19, 2016 at 02:05 PM 0
Share

Try

 collinf = GameObject.Find ("Giocatore").transform.Find("CollisoreInferiore").gameObject;

Or better still, make collinf a Transform ins$$anonymous$$d of a gameobject and use:

 collinf = GameObject.Find ("Giocatore").transform.Find("CollisoreInferiore");
avatar image Numisi · Aug 20, 2016 at 10:48 AM 0
Share

meat5000 thank you for your reply, but trying the first way it makes the same error, the second way it says this: "NullReferenceException: Object reference not set to an instance of an object playerBehaviour.Update () (at Assets/Scripts/playerBehaviour.js:44)"

avatar image shadowpuppet · Aug 20, 2016 at 07:25 PM 0
Share

in Java I never had to add the parent to the object i am looking for ( that's what it looks like in your script). I just do collinf = GameObject.Find ("CollisoreInferiore").Is collisoreinferiore a component or a child or what

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Numisi · Aug 20, 2016 at 10:04 PM

Now it is a child. I tried to use the GameObject.Find in either way: making it a child and separating it (obviously I changed the "Giocatore/CollisoreInferiore" stuff when it wasn't a child). I made it a child so it moves toghether, I should use it as a proximity trigger in a specific position. Today I tried to use the find with tag command and it worked perfectly at the first try. This only makes me more confused about why this doesn't work.

Comment
Add comment · Show 1 · 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 shadowpuppet · Aug 21, 2016 at 01:05 AM 0
Share

well maybe because it is a java script. I toiled for hours trying to figure out why the changes I made weren't doing anything...then suddenly it kicked in after I went on to something else for a while

avatar image
0

Answer by 1573680378 · Mar 14, 2017 at 05:07 PM

Maybe you should try collinf = GameObject.Find ("/Giocatore") lt's working for me. like chinese 玄学


Comment
Add comment · 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

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

6 People are following this question.

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

Related Questions

Set public gameobject by raycast hit target 1 Answer

my script was written but does not want to work 1 Answer

Displaying a different sprite depending on which angle a character is viewed from 1 Answer

When I change a bool in a script on one object it doesn't change the bool on the same script on another object? 1 Answer

Measuring/finding the height of a stack of game objects then recording that number. 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