- Home /
Question by
Jonnyboy111 · May 08, 2013 at 07:14 PM ·
instantiateprefabpositionparenting
Prefab instantiating problem
Hi, I'm having an issue with functionality in my code. I'm trying to make a shield prefab spawn on and travel with the player prefab by making it it's child. I'll post my code below. Any help is much appreciated.
public GameObject PlayerPrefab;
public GameObject Shield;
void OnTriggerEnter(Collider other)
{
//GameObject playerClone = GameObject.Find("Player(Clone)");
if (other.gameObject.name == "Player(Clone)")
{
//PlayerScript playerScript;
//playerScript = other.gameObject.GetComponent<PlayerScript>();
//playerScript.Score += 100;
GameObject newOne = (GameObject)Instantiate(Shield, PlayerPrefab.transform.position, PlayerPrefab.transform.rotation);
newOne.transform.parent = GameObject.Find("Player(Clone").transform;
}
}
Comment
Answer by Adamcbrz · May 08, 2013 at 07:20 PM
It looks like you should be setting the position and rotation based on the player's transform and not on the playerprefab.
Transform player = GameObject.Find("Player(Clone)").transform
GameObject newOne = (GameObject)Instantiate(Shield, player.position, player.rotation);
newOne.transform.parent = player;
Your answer
Follow this Question
Related Questions
How do I get the global position 2 Answers
How to follow multiple clones positions of an instantiate prefab having a velocity ? 1 Answer
Wrong particle and prefab spawn position 1 Answer
Instantiating an Object doesn't use the given position. 1 Answer
How to make an Instantiated prefab a Child of a gameobject that already exists in the hierarchy 3 Answers