• 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
Question by Wizeon · Jan 15, 2010 at 01:07 AM · gameobjectinstantiategetcomponent

Accessing instantiated GOs nested in prefabs with GetComponent

I asked this on the forums too in a multi-question post but haven't gotten answer to this yet. So I figured someone else might need this information too and add it to unityAnswers.

So the question, how do I access prefabs that are created with Instantiate and have nested GameObjects.

I'm instantiating the object on runtime with

var house1:GameObject = Instantiate(Resources.Load("HouseGreenSmall"), Vector3(5,-0.15,7), Quaternion.Euler(0,180,0));


I'm not entirely sure if they even are GOs or prefabs. You can see the object in the project view, hierachy view and the script doorlink attached to DoorTrigger GO (or prefab?). I've renamed the Door Trigger to DoorTrigger to avoid any confusion with the space, the screenshot is just from the old version.

I've tried several syntaxes but everything produces some error.

(by the way the accessthis() function is just for debugging)


house1.GetComponent(doorlink).accessthis();

produces NullReferenceError


print(house1.transform.position.x);

works, it prints the correct coordinate, but the coordinate of the GreenHouseSmall prefab.


print(house1.anything.transform.position.x);

produces " 'Anything' is not a member of 'UnityEngine.GameObject "


Wow this question is pretty messed up. Sorry for the inconvenience :|

Comment
Cyclops

People who like this

1 Show 0
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

4 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Jaap Kreijkamp · Jan 15, 2010 at 02:04 AM

Don't follow your story exactly but I believe you want to find a child of a GameObject/Transform.

The easiest way to do it is through the Transform.Find method. So say you have a GameObject named House with a child GameObject named Door. In the script attached to House:

...
var doorTransform : Transform = transform.Find("Door");
var doorGO : GameObject = doorTransform.gameObject;
...

You could consider making doorTransform and/or doorGO global variables and setting their values in the Awake function for performance reasons if you need to access the values often. This would result in:

private var doorTransform : Transform; private var doorGO : GameObject;

function Awake() { doorTransform = transform.Find("Door"); doorGO = doorTransform.gameObject;

}

now you can just use doorTransform and doorGO in your functions.

Comment
Wizeon
Cyclops

People who like this

2 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 Wizeon · Jan 15, 2010 at 02:16 AM 0
Share

I'm terribly sorry but I didn't remember to tell the vital part that I'm instantiating the whole thing on runtime. See the third paragraph, just added it. The thing I'm asking is to run the accessthis function in the doorlink script.

avatar image Cyclops · Feb 05, 2010 at 03:01 AM 0
Share

I have a similar problem, trying to reference nested GameObjects. However, the suggestion transform.Find("oneBone") does not work. Nor does transform.Find("/oneBone"), which the docs suggest a slash will cause Find to traverse the hierarchy. Any further answers?

avatar image

Answer by Lucas Meijer 1 · Jan 15, 2010 at 02:05 AM

in javascript,

house.GetComponent(DoorLink).accesthis()

should work. Please note that "DoorLink" and "acccesthis" are case sensitive.

PS Prefabs are gameobjects. The only difference between a gameobject in your scene, and a prefab is that one is saved in the scenefile, and the other is saved into a .prefab file in your Assets folder.

Comment
Wizeon

People who like this

1 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 Wizeon · Jan 15, 2010 at 02:19 AM 0
Share

Actually that produces NullReferenceException, just as house1.GetComponent(doorlink).accessthis(); If you see the screenshot the script is called doorlink, not DoorLink. I'm not sure if that matters. And see the edit I made, also explained it in the comments of above answer. This question turned out really bad, I'm terribly sorry.

avatar image Jaap Kreijkamp · Jan 15, 2010 at 03:02 AM 0
Share

Have you checked if the house variable has a value, what Lucas suggested should work, but indeed DoorLink must match the name of the javafile exactly (case sensitive), so in your case it would be 'doorlink'. Try with a debug statement to see if house has indeed a value.

avatar image

Answer by Ryuuguu · Jan 15, 2010 at 02:58 AM

In the screen shot, doorTrigger is a child game object of houseGreenSmall and doorlink is a componet of doorTrigger.

so stealing some code from Jaap Kreijkamp's and Lucas Meijer's answers

var doorTransform : Transform = transform.Find("DoorTrigger");
doorTransform.GetComponent(DoorLink).accesthis()
Comment
Wizeon

People who like this

1 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 Wizeon · Jan 15, 2010 at 03:44 AM 0
Share

This sounds promising. Still, I'd like to be able to "spawn" many clones of the house with js and the doors to lead to different levels, so I'd like to be able to do something like house1.GetComponent(DoorTrigger.GetComponent(doorlink).accessthis()) Do I make any sense?

avatar image Ryuuguu · Jan 17, 2010 at 03:42 AM 0
Share

Your code house1.GetComponent(DoorTrigger.GetComponent(doorlink).accessthis()) can be broken down to x =DoorTrigger.GetComponent(doorlink).accessthis(); house1.GetComponent(x); where is a type. what you want is house1.transform.Find("DoorTrigger").GetComponent(doorlink).accessthis(); or if you have only one DoorLink component under house1 house1.GetComponentInChildren(DoorLink).accessthis(); if you have more than one DoorLink it gets the first in depth first search.

avatar image

Answer by Cyclops · Feb 05, 2010 at 05:02 PM

Jaap's first answer does work. I had to experiment with it a bit, though, as Find doesn't work quite as I expected. Apparently it only finds direct children GameObjects, and not, say, grandchildren. :)

I had an imported model asset called anArm, which had nested GameObjects as anArm/skeleton/oneBone/twoBone.

GameObject tmpArm = (GameObject)Instantiate(anArm, thePos, Quaternion.identity);

Given that reference, I first tried this code:

Transform lastBone = tmpArm.transform.Find("twoBone");

It didn't work, either with or without a slash "/twoBone". However, when I included the complete child list:

Transform lastBone = tmpArm.transform.Find("skeleton/oneBone/twoBone");

it worked correctly, and I was able to access the last GameObject. hied, in the OP, your screenshot doesn't show nesting of more than one level, so the Find should have worked for you. Did it solve your problem?

John C>
"For all your days, prepare, and meet them ever alike;
When you are the anvil, bear - when the hammer, strike."

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

1 Person is following this question.

avatar image

Related Questions

Only change a variable on the instaniated object not the prefab. 0 Answers

Limiting respawns on scene 1 Answer

Get unity to recognize prefab in C# 2 Answers

Reference an Instance of an Object?C# 1 Answer

Instantiate GameObject, GetComponentsInChildren Cast Type Error - JS 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