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 /
This question was closed Apr 08, 2018 at 03:03 PM by meat5000 for the following reason:

Other

avatar image
0
Question by Xentarok · Oct 06, 2013 at 11:39 AM · cameraplayerprefsrtslevelsgameobject.find

NullReferenceException from GameObject.find

Hello.

So, I'm working on a space RTS game, where I have two levels: one is the galaxy view and another is a system view. I can freely move camera in the galaxy level, and when i click on a star it takes me to the system level. The idea is, that when I click on a star I want to save the camera position in PlayerPrefs using the following code:

 void OnMouseUp(){
         
         
         GameObject RP = GameObject.Find("RotationPoint");    
         
         GameObject parent = GameObject.Find("Parent");
         
         Camera Cam = Camera.main;
         
     float RPX = RP.transform.position.x;
     float RPY = RP.transform.position.y;    
     float RPZ = RP.transform.position.z;
         
     float PX = parent.transform.position.x;    
     float PY = parent.transform.position.y;            
     float PZ = parent.transform.position.z;    
         
     float CX = Cam.transform.position.x;    
     float CY = Cam.transform.position.y;
     float CZ = Cam.transform.position.z;
         
     float RPRX = RP.transform.rotation.x;    
     float RPRY = RP.transform.rotation.y;
         
     PlayerPrefs.SetFloat("RotPX",RPX);
     PlayerPrefs.SetFloat("RotPY", RPY);
     PlayerPrefs.SetFloat("RotPZ", RPZ);    
         
     PlayerPrefs.SetFloat("ParentX", PX);
     PlayerPrefs.SetFloat("ParentY", PY);
     PlayerPrefs.SetFloat("ParentZ", PZ);    
         
     PlayerPrefs.SetFloat("CamX", CX);
     PlayerPrefs.SetFloat("CamY", CY);
     PlayerPrefs.SetFloat("CamZ", CZ);        
         
     PlayerPrefs.SetFloat("RotX",RPRX);
     PlayerPrefs.SetFloat("RotY", RPRY);
         
     }

The problem is, that I get a NullReferenceError in the line with setting the Parent Pref. I've tried severeal things, and I think it's because of the GameObject.find. I also heard that I should avoid using it, but I can't really think of anything else(I've also tried .FindObjectWithTag).

Thanks in advance for all of the answers.

-Xentarok

P.S - My english isn't really good, so I apologize for any mistakes. P.S.2 - If you have any specific questions, write them in the comments.

Comment
Add comment · Show 1
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 The-Game-Master · Jun 15, 2015 at 04:12 PM 0
Share

I realize that this is 2 years old, but your English is great, actually.

3 Replies

  • Sort: 
avatar image
2

Answer by aldonaletto · Oct 06, 2013 at 12:30 PM

1) Yes, you should avoid any Find functions in Update - they are too slow, specially when there are many GameObjects in scene. Declare a public variable and drag the object to it in the Inspector, or Find it only once at Start:

 public GameObject RP; // drag the object here in the Inspector...
 
 void Start(){
   if (!RP){ // if RP not assigned in the Inspector...
     RP = GameObject.Find("RotationPoint"); // Find it 
   }
 }

2) About the Parent: apparently, there's no object named "Parent" in scene, thus Find returns null and originates this error - check its capitalization ("parent" isn't the same as "Parent"). Or are you trying to access the parent of some object? If so, that's the wrong way: someObj.transform.parent gives you the parent of someObj :

 void Update(){
   Transform rpParent = RP.transform.parent;
   ...

3) transform.rotation is a Quaternion, not those nice angles we see under Rotation in the Inspector (they are actually the transform.eulerAngles ). Anyway, saving only X and Y won't work fine in both cases: you must save all XYZW components of rotation, or XYZ of eulerAngles. By the way, you can save stuff in a much easier and efficient way - like below:

 // cache the properties in temporary variables:
 Vector3 rpPos = RP.transform.position;
 Vector3 pPos = parent.transform.position;    
 Vector3 camPos = Cam.transform.position;
 Quaternion rpRot = RP.transform.rotation;
 
 PlayerPrefs.SetFloat("RotPX", rpPos.x);
 PlayerPrefs.SetFloat("RotPY", rpPos.y);
 PlayerPrefs.SetFloat("RotPZ", rpPos.z);    
 
 PlayerPrefs.SetFloat("ParentX", pPos.x);
 PlayerPrefs.SetFloat("ParentY", pPos.y);
 PlayerPrefs.SetFloat("ParentZ", pPos.z);   
 
 PlayerPrefs.SetFloat("CamX", camPos.x);
 PlayerPrefs.SetFloat("CamY", camPos.y);
 PlayerPrefs.SetFloat("CamZ", camPos.z);   
 
 PlayerPrefs.SetFloat("RotX", rpRot.x); // you must save all rotation components...
 PlayerPrefs.SetFloat("RotY", rpRot.y);
 PlayerPrefs.SetFloat("RotZ", rpRot.z); // including Z...
 PlayerPrefs.SetFloat("RotW", rpRot.w); // and W

NOTE: Remember to restore all components of rotation too!

Comment
Add comment · Show 4 · 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 Xentarok · Oct 06, 2013 at 12:47 PM 0
Share

Thanks for the answer but unfortunetly it doesn't work. This script is attached to a prefab so i can't assign the "Parent" in the inspector, and when I have the .find in the start, it still gives me an error! About 1 nad 2, I have an object called Parent and I only need two elements of the rotation.

avatar image aldonaletto · Oct 06, 2013 at 04:10 PM 0
Share

You must save the entire rotation or eulerAngles - all the XYZW components of rotation are necessary to define a Quaternion, and the eulerAngles may have weird values in X and Y that are compensated by another weird value in Z: if you set (120,120,0) in the Inspector, for instance, it becomes (60,300,180) when running - if you save only X and Y, the restored angles will become (60,300,0) ins$$anonymous$$d of (120,120,0)!

From the comments you've posted in @Vandash's answer, it seems that RotationPoint and Parent don't exist anymore when you try to save them! Add debug lines to check this:

 void On$$anonymous$$ouseUp(){ 
    GameObject RP = GameObject.Find("RotationPoint"); 
    if (!RP) Debug.Log("RotationPoint not found");
    GameObject parent = GameObject.Find("Parent");
    if (!parent) Debug.Log("Parent not found");
    ...

If you're about to load another scene, your logic may be wrong and those objects have already been destroyed at On$$anonymous$$ouseUp - try to use On$$anonymous$$ouseDown ins$$anonymous$$d.

avatar image Xentarok · Oct 06, 2013 at 04:57 PM 0
Share

Yeah, both of these Debugs were shown, I tried On$$anonymous$$ouseDown but it doesn't work either.

avatar image aldonaletto · Oct 06, 2013 at 04:59 PM 0
Share

So, these objects don't exist anymore. Check your logic: something is destroying them before.

avatar image
1

Answer by David_29 · Jun 07, 2017 at 02:54 AM

Quickfix: if you're using C#, try adding this code using Systemat the top of your script or import System if you're using Javascript. You can now try-catch GameObject.Find if it returns null. No errors will throw at you.

Here's an example:

 using System; // --> This exact line of code. That's it.
 using UnityEngine;
 
 public class Test : MonoBehaviour {
 
     public void OnMouseUp() {
 
         // You may now catch null reference here.
         try {
 
             GameObject RP = GameObject.Find("RotationPoint");

                     // The rest of your code here.
 
         } catch(NullReferenceException e) {
 
         }
 
     }
 
 }

Also remember, you can catch also other exceptions such as MissingReferenceException, MissingComponentException, IndexOutOfRangeException, or any other exception classes as long as you include using System in your script. That is all.

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
avatar image
0

Answer by Vandash · Oct 06, 2013 at 01:10 PM

Do you have a gameobject in your scene called "RotationPoint" and one called "Parent" ? GameObject.Find will look for object names, so you need to make sure these objects actually exist in your scene.

  • On a side note, you should rename all your variable to give them more reliable names.

  • I would avoid having one game object called "parent", as the parenting concept is usually used by Unity in transform.parent (which makes your naming confusing), instead try using Root or World or something similar.

Comment
Add comment · Show 13 · 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 Xentarok · Oct 06, 2013 at 01:20 PM 0
Share

Yes, I have an both of these objects, named exactly like in the script.

avatar image Vandash · Oct 06, 2013 at 01:24 PM 0
Share

In your other comment you talked about prefabs. Gameobject.Find will NOT find prefabs (except if they are instanciated in the scene). Could that be the problem ?

avatar image Xentarok · Oct 06, 2013 at 01:31 PM 0
Share

No, the script is attached to a instanciated prefab but the objects are in scene by default.

avatar image Vandash · Oct 06, 2013 at 01:34 PM 0
Share

I think you failed at explaining your problem because this sounds like a super simple problem... - Is your parent variable null ? - On what line is your error co$$anonymous$$g exactly ?

avatar image Xentarok · Oct 06, 2013 at 01:37 PM 0
Share

As I said before, the error comes from the line where i set the float to the position on x axis of parent which is a GamaObject defined by GameObject.Find, and since I don't get that error from the Camera (Cam) I assume that it's a problem with gameobject.find.

Show more comments

Follow this Question

Answers Answers and Comments

19 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

Related Questions

RTS Mouse Click Movement 1 Answer

SmoothDamp bounce effect 0 Answers

RTS game: Camera movement is wierd 2 Answers

3D RTS camera 3 Answers

How to save progress in the game? 1 Answer


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