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 Tanksy · Dec 17, 2013 at 09:09 PM · c#transformpositionvector3

Trouble setting up a vector 3.

I'm trying to create a vector3 variable that is equal to the position of a child object using this code:

             Transform doorPointPosition = 
             this.gameObject.transform.Find("DoorPointNorth");
 
             Vector3 doorPoint = (doorPointPosition.position);

DoorPointNorth is the name of the child that I'm trying to get the position of, so that I can use it as a vector3 position later.

For some reason I keep getting a Null reference error. I've checked through the code and the name I've specified for the transform.Find is definitely correct. What am I doing wrong here, and how could I fix it?

Any help would be appreciated, thanks.

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 Bunny83 · Dec 17, 2013 at 10:27 PM 1
Share

Your code actually should work. Can you post how your hierarchy looks like and to which object in the hierarchy this script is attached to?

Are you sure that "DoorPointNorth" is a direct child of the object this script is attached to?

btw: This is double redundant:

     this.gameObject.transform.Find(

it's the same as

     transform.Find(
avatar image Tanksy · Dec 17, 2013 at 11:48 PM 0
Share

Thanks, That helps make the code look a little neater. The problem's actually been solved. I might of misunderstood what parenting and children are - as simply using GameObject.find to get the DoorPointNorth worked fine.

avatar image ThatsAMorais · Dec 18, 2013 at 05:26 AM 0
Share

An understandable mistake, but once you know about the distinction its actually convenient to have both a wide search with GameObject.Find and a shallower search with transform.Find().

1 Reply

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

Answer by ThatsAMorais · Dec 17, 2013 at 09:23 PM

Perhaps the object you're looking for is not a child of "this". I would suggest you use.

 GameObject.Find("DoorPointNorth");
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 Tanksy · Dec 17, 2013 at 09:46 PM 0
Share

This worked, but now I'm worried - Due to how I have several prefabs each with their own "DoorPointNorth" object, how will I be able to specify which prefab's "DoorPoint" it is that I'm using?

avatar image BotHH · Dec 17, 2013 at 10:11 PM 0
Share

You can add tags to game objects.

 GameObject.FindGameObjectsWithTag("DoorPointNorth");

Tag each DoorPointNorth with a different tag.

avatar image Tanksy · Dec 17, 2013 at 11:50 PM 0
Share

I've managed to set up a workaround similar to what you suggested, only I've set the code to rename each object to something with a unique number on the end when it's instantiated, and that works fine.

It might be horribly bad coding, or inefficient as all hell but it works, so I'm not so bothered right now

avatar image ThatsAMorais · Dec 18, 2013 at 05:12 AM 0
Share

I often use hierarchy. The way you "set parent" is through the transform. So, I would create the DoorPointNorth from the object with which its associated, and then assign the parent like this.

 doorPointInstance.transform.parent = transform;

That will set the parent of DoorPointInstance to the transform that created it. At the very least you can use gameObject.Find("") to get it, but I would store it and make a public accessor function.

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

20 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

Related Questions

Issue's With Positioning 0 Answers

Guided missile help c# 1 Answer

How to store position in a variable and than edit only specific part of it(such as 'x') 1 Answer

How to rotate an object to face the direction it's going? 1 Answer

Script to make Camera follow the player C# 3 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