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 /
avatar image
0
Question by hexagonius · Jun 16, 2016 at 02:30 PM · prefabchildreferencerecttransformnullreference

Retrieving RectTransfrom from null reference possible???

I stumbled across a very strange behaviour. I received a reference to a RectTransform of an uninstantiated and therefore null gameobject. I wanted to read the extends from a child RectTransform of a prefab without instantiation. The following code did that:

     public static Vector2 GenerateMeasureForMessage (ES_Message message){
 
         RectTransform messageRect = message.transform.GetChild (0).GetComponent<RectTransform> ();
         return messageRect.offsetMin - messageRect.offsetMax;
 }

the following screenshot shows, that, since I use the "message" prefab directly, all its MonoBehaviour variables are still null.

hovering over the message prefab reference

The next screenshot however shows, that it was still possible for the code or Unity or... to retreive the reference

hovering over the messageRect reference

I stumbles across this, because I moved the code into another static variable, doing the exact same stuff, just relaying the work to another static method and THERE it threw the NullReferenceException.

The weird stuff does not happen in the class that holds the prefabs, it's already a different class.

I don't get how this works.

screen-shot-2016-06-16-at-161944.png (109.9 kB)
screen-shot-2016-06-16-at-161916.png (106.6 kB)
Comment
Add comment · Show 2
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 NoseKills · Jun 16, 2016 at 04:16 PM 0
Share

Hard to say much without knowing more. What do you mean by 'uninstantiated and therefore null gameobject' ? If message a variable that has never been assigned, then surely accessing any of it's members would throw an exception. If it's something loaded with Resources.Load or something assigned in inspector or created with new GameObject(), then it isn't null.

Unity objects are wrappers for underlying c++ objects and they are made to appear null in a convenient way. That's why referencing a (see$$anonymous$$gly) null Unity.Object doesn't necessarily throw a NullReferenceError, but a '$$anonymous$$issingComponentException' or a 'UnassignedReferenceException'. Also it is possible for 'this' to be null inside a $$anonymous$$onoBehaviour.

This in combination with the fact that the debugger doesn't always show the whole truth accurately could well make things seem weird like this i guess :)

avatar image hexagonius NoseKills · Jun 21, 2016 at 07:45 AM 0
Share

Hey, thanks for the answer. By "uninstantiated" I meant accessing the [SerializeField] variable directly. And it is assigned in the inspector.

1 Reply

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

Answer by Owen-Reynolds · Jun 16, 2016 at 05:25 PM

I think "uninstantiated and therefore null" is the problem. AFAIK, uninstantiated prefabs are real gameObjects, which just aren't in the scene (and never will be.) You can examine them like anything else. As NoseK writes, the debugger looks to be wrong.

I remember it was even common to change parts of a prefab by mistake, with no harm done. Many Qs asked why a=Instantiate(b); b.thisAndThat=wasn't changing their scene object a. No errors -- just nothing happened. And I can print transform.position directly from my prefabs.

Sure, it's a get, and not a variable, but I think the debugger calls it like normal -- any funny unpacking prefab stuff should work either way. Maybe it's not fully set-up(?) (but then why would it work in picture #2?) Maybe there's also an issue specific to RectTransforms. Maybe.

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 hexagonius · Jun 21, 2016 at 07:48 AM 0
Share

Thanks Owen for the rundown. I remember the same thing about changing prefab variables from code. It's just weird, that running the code works, but putting it into yet another static method works different and throws the exception.
$$anonymous$$aybe I get to understand that at some point, but for now I think your answer suffices

avatar image Owen-Reynolds · Jun 21, 2016 at 09:19 PM 0
Share

Ah ... I didn't even notice the part about it working one way, but not the other. AFAI$$anonymous$$, running something through a static method or a member function shouldn't matter (I think you can't start a coroutine from a static, but you're not doing that, are you?)

$$anonymous$$y guess is that one version just happened to be called before the message argument was created. If you can get it to work/notWork simply by changing a function to static and back, that would be a real mystery.

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

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

Related Questions

Make a simple tree 1 Answer

Clicking on a prefab object in scene view either selects ground behind it or the prefab child 1 Answer

change/scale child rectTransform with Parent (with video) 0 Answers

Prefab from file reference not cloned? 1 Answer

Instantiate as children of GameObject hit by raycast. 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