Is there an easy way to instantiate an object that sticks to the player? So that the player can pick something up and carry it around with it staying in view? Thanks.
probably the simplest way would be: Code (csharp): // instantiate the object myObject = Instantiate(prefab); // make the object a parent of the player myObject.transform.parent = thePlayer.transform; // if you want to, change the object's location to move somewhere else, like the player's back myObject.transform.localPosition = newLocalPosition;
there's also this script from a while back which might help: http://forum.unity3d.com/viewtopic.php?t=1597&highlight=grenade
I didn't have a chance to try this until now, but I am running into a problem. I thought the "(prefab)" in the script above would be a variable that a prefab is assigned to so I had this: Code (csharp): var key : Transform; var player : Transform; followed after other code by this: Code (csharp): myObject = Instantiate(key); myObject.transform.parent = player.transform; And now Untiy won't let me assign the prefab to that variable anymore. It only lets me assign something that is already in the game and the object I want to use is not yet in the game. Any idea what I am doing wrong? Thanks
Both of the objects will need to be in the game in order to change the transform parent. A little more detailed explanation of what you're trying to do would be helpful Your variable declarations should be: Declaring the variables as Transforms, the trying to access the transform from within the transform is why Unity is getting confused. Try that out and see if it works.
My game was broken for about a month and I have finally gotten it mostly fixed and can turn back to this issue. I am no longer instantiating the object I want to "stick" to the player. I now just need to have the player "carry" an object that it runs into. I tried making the object a child of the player with this script below but got the warning "An instance of type 'UnityEngine.Component' is required to access non static member 'transform'. Code (csharp): transform.parent = Player.transform; The script is attached to the object I want the player to carry. Any ideas?
What is "Player"? Are you sure it's not 'player' or 'thePlayer' as above? It sounds like it thinks you're referring to a class (another script) called "Player."
"Player" is actually the name of my player in the Hierarchy. Using this name works elsewhere in the scripts I am using but maybe this is a special case...
is Code (csharp): var Player : GameObject; declared at the top of the script and assigned in the inspector?