Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Collider Error

Discussion in 'Scripting' started by niick555, Sep 19, 2014.

  1. niick555

    niick555

    Joined:
    Sep 19, 2014
    Posts:
    11
    Good night,
    I'm new in the area of programming and am having some problems with the collider. The game runs, but even when the player collides with the "Currencies", they are not destroyed or become inactive. (I tried both ways, left a comment way). Know tell me where is the error?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Personagens : MonoBehaviour {
    5.     public GameObject personagem;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.     }
    11.  
    12.     // Update is called once per frame
    13.     void Update () {
    14.         if (GameObject.FindGameObjectWithTag ("Personagem") == false)
    15.         {
    16.             Vector3 posicaoNovoObjeto = new Vector3 (0, 2, 0);
    17.             Quaternion rotacaoNovoObjeto = new Quaternion (0, 0, 0, 0);
    18.          
    19.             Instantiate (personagem, posicaoNovoObjeto, rotacaoNovoObjeto);
    20.         }
    21.     }
    22.  
    23.     void OnTriggerEnter (Collider other)
    24.     {
    25.         if (other.gameObject.tag == "Moedas")
    26.         {
    27.             //Destroy (other.gameObject);
    28.             other.gameObject.SetActive(false);
    29.         }
    30.     }
    31. }
    I also tried the following way, and kept having problems:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class Personagens : MonoBehaviour {
    5.     public GameObject personagem;
    6.     public GameObject moedas;
    7.  
    8.     // Use this for initialization
    9.     void Start () {
    10.  
    11.     }
    12.  
    13.     // Update is called once per frame
    14.     void Update () {
    15.         if (GameObject.FindGameObjectWithTag ("Personagem") == false)
    16.         {
    17.             Vector3 posicaoNovoObjeto = new Vector3 (0, 2, 0);
    18.             Quaternion rotacaoNovoObjeto = new Quaternion (0, 0, 0, 0);
    19.          
    20.             Instantiate (personagem, posicaoNovoObjeto, rotacaoNovoObjeto);
    21.         }
    22.     }
    23.  
    24.     void OnTriggerEnter (Collider moedas)
    25.     {
    26.         //Destroy (moedas.gameObject);
    27.         moedas.gameObject.SetActive(false);
    28.     }
    29. }
    30.  
    Thank you so much!
     
  2. niick555

    niick555

    Joined:
    Sep 19, 2014
    Posts:
    11
    I managed to find the error. The problem was that I have the gameObject character inside another empty gameObject, and I was putting the script in the empty gameObject.
     
  3. JamesLeeNZ

    JamesLeeNZ

    Joined:
    Nov 15, 2011
    Posts:
    5,616
    this is very bad code to use in update

    GameObject.FindGameObjectWithTag("Personagem")


    cache it.
     
  4. niick555

    niick555

    Joined:
    Sep 19, 2014
    Posts:
    11
    And how could I do to get better? 'm Still beginner in Unity and C #.