• 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 Jopz · Apr 01, 2011 at 09:52 AM · instantiateparent

Instantiate a prefab to a Parent?

I have a game object as a parent and gui texture as its child.

I want to put a particle(named Hint) in a game object(parent of gui texture) when i clicked the gui texture. since its positioning is ruined because of the parent-child relation.

this is my code...


var Hint: GameObject;

function OnMouseOver() {

if(Input.GetMouseButtonDown(0)) {
Instantiate(Hint, transform.parent.gameObject.position, Quaternion.identity);
}

}


it display this message : "'position' is not a member of 'UnityEngine.GameObject'. "

can anyone help me? x.x

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 mangoblue · Mar 11, 2012 at 12:42 PM 0
Share

try this:

 instantiatedHint.transform.localPosition = transformGameObjectTo$$anonymous$$atch.transform.localPosition;

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by allenziff · Oct 29, 2012 at 10:06 AM

try this

 var Hint: GameObject;
 var spawnpoint: Transform;
 var HintParent: Transform;
 
 function OnMouseOver() {
 
 if(Input.GetMouseButtonDown(0)) {
 var HintInsta: GameObject = Instantiate (Hint,spawnpoint.transform.position,Quaternion.identity);
 HintInsta.transform.parent = HintParent.transform;
 }
 }
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 mangoblue · Mar 11, 2012 at 12:59 PM

 instantiatedHint.transform.localPosition = transformGameObjectToMatch.transform.localPosition;
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
1

Answer by e-bonneville · Apr 01, 2011 at 10:54 AM

You'll just want to do this:

var Hint: GameObject; var parentOfHint : Transform;

function OnMouseOver() {

 if(Input.GetMouseButtonDown(0)) {
     var instantiatedHint = Instantiate(Hint, transform.position, Quaternion.identity);
     instantiatedHint.transform.parent = parentOfHint;
 }

}

In the script above, the parentOfHint is the Transform you want to parent Hint to. The second variable in the Instantiate function is actually where you want the object to be positioned upon creation, so you can't set its position at the same time, or Unity will get confused.

Please note that the above script is untested and may contain errors.

Comment
Add comment · Show 8 · 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 Jopz · Apr 01, 2011 at 11:32 AM 0
Share

I've tried your code and it says "$$anonymous$$issingFieldException: Field 'UnityEngine.GameObject.parent' not found."

instantiatedHint.parent = parentOfHint; <- that is where it point the error

avatar image e-bonneville · Apr 01, 2011 at 11:50 AM 0
Share

Oops, you're right. Line instantiatedHint.parent = parentOfHint; should actually read instantiatedHint.transform.parent = parentOfHint; I'll edit my answer to reflect the correction. :)

avatar image Jopz · Apr 01, 2011 at 12:09 PM 0
Share

yup, I've also tried that code but it still doesn't work, I also debug by using print inside the $$anonymous$$oustButtonDown and it printed the string i assigned. so that means he's entering the condition but not instantiating... x.x

avatar image e-bonneville · Apr 01, 2011 at 12:27 PM 0
Share

Well, I don't know what to say then. Is Hint assigned? How do you know that it isn't being instantiated? Check in the Hierarchy view after instantiation during gameplay and see if it's there. It may be being instantiated, just not where you think it is. It will be instantiated at the same position as the owner of the script.

avatar image Jopz · Apr 01, 2011 at 02:02 PM 0
Share

its appearing, but its way too off in the object... it isn't placing to the parent, in the Hierarchy it displays Hint(clone) and it is place as a child along with the texture

Show more comments
avatar image
1

Answer by StephanK · Apr 01, 2011 at 10:44 AM

Position is a member of Transform. So you can access the parents position using this: transform.parent.position

However in my experience having GUITextures as children of other objects isn't a good idea, at least if the parent is supposed to move around as GUITextures use ScreenCoordinates and everything else is in local Coordinates.

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

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

2 People are following this question.

avatar image avatar image

Related Questions

Parenting an instantiated Prefab? 1 Answer

Change Parent of Multiple Instantiated Objects 1 Answer

Howto set main.camera as a parent when main.camera itself is a child of a prefab 1 Answer

Instantiated GameObject collision without script repetition? 1 Answer

Problem adding a Parent to a Instantiated Object 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