- Home /
Spawn Gameobject at Cloned Gameobjects gameobjects
I want to spawn an object at a cloned gameobjects 4 gameobjects positions. My code right now will spawn 4 watermelon_split object at the watermelon clone, but not at the watermelon_split1 position or watermelon_split2 and etc.
using UnityEngine;
using System.Collections;
public class attackTrigger : MonoBehaviour {
public GameObject watermelon;
public GameObject watermelon_split;
public Transform watermelon_split1;
public Transform watermelon_split2;
public Transform watermelon_split3;
public Transform watermelon_split4;
void OnTriggerEnter2D(Collider2D col)
{
if(col.isTrigger != true && col.CompareTag("watermelon"))
{
Instantiate(watermelon_split, col.transform.position, watermelon_split1.rotation);
Instantiate(watermelon_split, col.transform.position, watermelon_split2.rotation);
Instantiate(watermelon_split, col.transform.position, watermelon_split3.rotation);
Instantiate(watermelon_split, col.transform.position, watermelon_split4.rotation);
Destroy(col.gameObject);
}
}
}
new-pic.png
(35.6 kB)
Comment
Best Answer
Answer by Youri1er · Aug 18, 2015 at 02:38 PM
Hi try to find child to spawn your splits_watermelon.
using UnityEngine;
using System.Collections;
public class attackTrigger : MonoBehaviour
{
public GameObject watermelon;
public GameObject watermelon_split;
void OnTriggerEnter2D(Collider2D col)
{
if(col.isTrigger != true && col.CompareTag("watermelon"))
{
Instantiate(watermelon_split, col.transform.GetChild(0).transform.position, col.transform.GetChild(0).transform.rotation);
Instantiate(watermelon_split, col.transform.GetChild(1).transform.position, col.transform.GetChild(1).transform.rotation);
Instantiate(watermelon_split, col.transform.GetChild(2).transform.position, col.transform.GetChild(2).transform.rotation);
Instantiate(watermelon_split, col.transform.GetChild(3).transform.position, col.transform.GetChild(3).transform.rotation);
Destroy(col.gameObject);
}
}
}
I don't know if you want this ?