Hi, is there a way from one script we can access to one gameobject which is created only on runtime , but to its children which are an array children ?
One way would be to store the children as they’re created.
Another would be to use transform.Find(name) or transform.GetChild(index).
1 Like
Could you please explain more ? Here are my codes : i want from gamecontroller script check when player collides with each of those array’s children something happens,some codes run i mean :
using UnityEngine;
using System.Collections;
public class GameController : MonoBehaviour {
public Spawner spawner;
public GameObject player;
public BackgroundScrolling bgs;
public ObjectPool objectpool;
public RecycleGameObject prefab;
void Awake()
{
spawner = GameObject.Find ("Spawner").GetComponent<Spawner>();
player = GameObject.Find ("Player");
prefab = GameObject.Find ("EnemyPrefab");
}
void Start ()
{
}
void Update ()
{
}
void OnCollisionEnter2D(Collision2D col)
{
}
}
public class ObjectPool : MonoBehaviour {
public RecycleGameObject prefab;
private List<RecycleGameObject> poolInstances = new List<RecycleGameObject>();
private RecycleGameObject CreateInstance(Vector3 pos){
var clone = GameObject.Instantiate (prefab);
clone.transform.position = pos;
clone.transform.parent = transform;
poolInstances.Add (clone);
return clone;
}
public RecycleGameObject NextObject(Vector3 pos){
RecycleGameObject instance = null;
foreach (var go in poolInstances) {
thanks .
No Answer?who knows?
If the parent has the rigidbody and the script with collision events, and the children have colliders, then the parent will detect all child collision events.
i used the codes below and its ok but player does not remain on spot which i want :
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Enemy")
{
StartCoroutine (retro ());
}
}
IEnumerator retro()
{
transform.localPosition = new Vector3(-4, -0.5f, 0);
var running = false;
anim.SetBool ("Running", running);
yield return new WaitForSeconds(1f);
Time.timeScale = 0;
gameoverPanel ();
}
public void gameoverPanel()
{
GameoverPanel.SetActive (true);
Buttons.SetActive (false);
}