- Home /
Prefab instantiated by code, collision does not work
What happens is that when I start my prefabs by the character code not collide with the obstacles. If I drag the character and objects on stage collisions usually work
See the settings of prefabs and codes below
My code:
Class Jogo
float jump = 20f; // Use this for initialization void Start () { gato = (GameObject)Instantiate(Resources.Load("gato")); StartCoroutine(Tap()); StartCoroutine(AddObstaculo()); }GameObject gato;
// Update is called once per frame void Update () { StartCoroutine(Tap()); } IEnumerator Tap() { while(Input.GetMouseButton(0)) { gato.gameObject.rigidbody2D.AddForce (transform.up jump ); yield return null; } } IEnumerator AddObstaculo() { while(true) { gameObject.AddComponent(); yield return new WaitForSeconds(2); } } > GameObject obj; string[] prefab = new string[5] {"mina", "asteroide0","asteroide1","asteroide2","corvoPreto"}; bool ativo = true; void Start () { StartCoroutine(NovoObstaculo()); } void Update () { if (ativo) { obj.transform.position += Vector3.left 5.0f Time.smoothDeltaTime; } } IEnumerator NovoObstaculo() { int prefabNumber = Random.Range (0, 5); // posição aleatoria do array prefab string novoObjt = prefab [prefabNumber]; // seleciona uma posição do array prefab float y = Random.Range (-4f, 5F); // posição Y aleatória da tela // obj = GameObject.Instantiate (Resources.Load (novoObjt), new Vector3 (8, 0, 0), Quaternion.identity) as GameObject; // insere o objeto na tela // yield return new WaitForSeconds (5); ativo = false; Destroy (obj); } Class Obstaculo > GameObject obj; string[] prefab = new string[5] {"mina", "asteroide0","asteroide1","asteroide2","corvoPreto"}; bool ativo = true; void Start () { StartCoroutine(NovoObstaculo()); } void Update () { if (ativo) { obj.transform.position += Vector3.left 5.0f * Time.smoothDeltaTime; } } IEnumerator NovoObstaculo() { int prefabNumber = Random.Range (0, 5); // posição aleatoria do array prefab string novoObjt = prefab [prefabNumber]; // seleciona uma posição do array prefab float y = Random.Range (-4f, 5F); // posição Y aleatória da tela // obj = GameObject.Instantiate (Resources.Load (novoObjt), new Vector3 (8, 0, 0), Quaternion.identity) as GameObject; // insere o objeto na tela // yield return new WaitForSeconds (5); ativo = false; Destroy (obj); }
captura de tela 2014-02-12 às 18.09.05.png
(332.6 kB)
captura de tela 2014-02-12 às 18.11.05.png
(322.2 kB)
Comment
Have you tried debuging collider settings after instantiat prefab?
Your answer
Follow this Question
Related Questions
2D Collision not working! (child sprites colliders) 1 Answer
Ignore collision at high velocity. 1 Answer
How do I get the player and the object they touch both disappear/destroyed? 1 Answer
Camera gets flung off of the map when the player collides with certain objects. 0 Answers
Punching a crate 0 Answers