The question is answered, right answer was accepted
Parent Problem
Personally, I have a big problem,
I am instantiating an object, and all good, instantiating normally, however when I put him in parent, it is in parent correctly, the problem is that it changes position.
Already tried: // Sets "newParent" as the new parent of the player GameObject. player.transform.SetParent (newParent);
And also I tried: // Makes the GameObject "newParent" the parent of the GameObject "player". player.transform.parent = newParent.transform;
I can not understand what's happening, because the biggest problem is it was working perfectly, and do not know what I modified.
Help me.
If you use "transform.localposition" Positions are relative to the parent game object's position, so when you try to instantiate your gameobject, it calculates the position relative to it's parent object. This answer may help you.
It's possible that the position of your prefab changed from all 0 to some value (which is annoying sometimes). These values are used when the object is instantiated. Further note: If e.g. the prefab has an x value of 10 and should use that when childed to a GO as its local position value, use the SetParent method with the second parameter set to false (this is default for assembling UI)
I tried to do that, but he does not recognize this command line. I have to add some library "using"?
It's an ordinary Trasform method. You can check the syntax here
https://docs.unity3d.com/ScriptReference/Transform.SetParent.html
the problem is that we have tried to do that way too, and even then the object appears in a very distant place, he saw the son, but the father ends up going to where it instantiates.
this is my script: if (Objetoem$$anonymous$$aos == null) {
Obj$$anonymous$$ = Instantiate (Inventory.ObjPrefab [NS], Position$$anonymous$$.position, Position$$anonymous$$.rotation) as GameObject;
Obj$$anonymous$$.transform.parent = PositionD$$anonymous$$.transform;
if I remove the parent command, the object appears exactly in the right place, but the object is a weapon and must accompany the camera.
Answer by Gamerdrill · Oct 26, 2016 at 05:55 PM
very personal,
not solved my problem, but I will try to do something, as we say here in Brazil, "make a kludge" what goes well I post here. I thank everyone and if anyone has any more idea can send.
I believe my prefab is wrong in some way, because when I put a cube zeroed in place, it works perfectly.
Where did you create that prefab? Go there and check the pivot point of your model. In $$anonymous$$aya you can center the pivot point and clear the history. You can also check your object's pivot point in Unity by changing the tool handle placement buttons (see image below). I think you have already tried all of these but worth to try.!
$$anonymous$$an, I think I wiggled it around when I was doing the prefab, I try to make another prefab later and see if I have not done anything wrong. Thanks
Follow this Question
Related Questions
Setting a parent of instantiated object negate all axis 1 Answer
Can't Instantiate an object as child of another 1 Answer
Instantiating as a child of an object in hierarchy 1 Answer
How to set lots of Instantiate prefab Gameobjects in the same parent? 1 Answer
Child with Rigidbody doesn't move with Parent with Rigidbody 3 Answers