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 Byterunner · Jun 24, 2010 at 09:32 PM · instantiatetransformnullreferenceexception

How can I get the position of transforms inside an instantiated game object?

Context: Placing customizable guns on a customizable base vehicle.

I have a game object with a script attached. The script contains variables for the base model and the guns model.

var baseFile : Transform; var gunFile : Transform;

function Start(){ // create base mesh newBaseModel = Instantiate(baseFile, transform.position, transform.rotation);

 // make a gun and position it
 gunLocator = newBaseModel.Find("gunLocation");
 print(gunLocator); // This line prints out fine
 print(gunLocator.position); // This line errors out
 newGun = Instantiate(gunFile, gunLocator.position, gunLocator.rotation);
 newGun.localScale = gunLocator.localScale;
 newGun.parent = transform;

}

This is all that's in my scene. When I hit play, I get the error:

NullReferenceException

VehicleBuilder.Start() (at Assets\Scripts\VehicleBuilder.js:11)

Once playing, I can look at my instantiated vehicle in the hierarchy, expand it out and find the "gunLocation" locator, and the line that prints properly prints out UnityEngine.Transform.

Any ideas why I can't access the position of the locator in an instantiated object?

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

1 Reply

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

Answer by Mike 3 · Jun 24, 2010 at 09:39 PM

It's not finding a transform, it's just sending back null, and the javascript compiler is fooled because it thinks gunLocator is supposed to be a Transform (and theres some weird voodoo which lets js print out Transform instead of null in cases like this)

My guess is that your gunLocation object is not a direct child of newBaseModel - if it's not, that function won't find it

It's path based, not search

If you need to search through the children, the easiest way is to make a quick recursive function

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 Byterunner · Jun 25, 2010 at 12:04 AM 0
Share

$$anonymous$$y prefab for the vehicle had an extra node between the model and the prefab's root. Removing that solved the problem as the model was now a the piece being instantiated, not the game object above the model. Thanks for the tip!

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

No one has followed this question yet.

Related Questions

Why am I getting a nullReferenceException? 1 Answer

Why does this Instantiate give a Null Reference Exception? 1 Answer

How to instantiate prefab over the text one letter at a time? OR how to get transform of letters in a text 0 Answers

How to Instantiate prefab as child? (Java) 1 Answer

Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 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