- Home /
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
try this:
instantiatedHint.transform.localPosition = transformGameObjectTo$$anonymous$$atch.transform.localPosition;
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.
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.
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
Oops, you're right. Line instantiatedHint.parent = parentOfHint;
should actually read instantiatedHint.transform.parent = parentOfHint;
I'll edit my answer to reflect the correction. :)
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
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.
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
Answer by mangoblue · Mar 11, 2012 at 12:59 PM
instantiatedHint.transform.localPosition = transformGameObjectToMatch.transform.localPosition;
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;
}
}
Your answer
Follow this Question
Related Questions
How to Instantiate prefab as child? (Java) 1 Answer
Setting parent of instantiated object fails (Error: setting parent of prefab is disabled...) 1 Answer
Trying to instantiate prefabs to a parent object with C# script. 1 Answer
[SOLVED] How to make to clone an object and make it parent of the transform? 1 Answer
Instantiated object not receiving value from parent class 2 Answers