Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Object won't get destroyed

Discussion in 'Scripting' started by spearbeard, Jan 20, 2019.

  1. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    I have several cubes which on collsion should dissapear. And then add points onto the a scroll view text prefab. But the problem is the cubes do not disseappear on trigger collision. Instead it spams the first location regardless enter several times in the scrollview Heres the scripts.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class LandmarkController : MonoBehaviour
    6. {
    7.     public LandmarksData[] ld;
    8.  
    9.     public GameObject itemPrefab;
    10.     public GameObject content;
    11.     public GameObject addedItem;
    12.  
    13.     public Animator menuAnim;
    14.  
    15.     public void AddItemToList(GameObject go)
    16.     {
    17.         addedItem = Instantiate (itemPrefab);
    18.         addedItem.transform.SetParent (content.transform);
    19.         addedItem.transform.localPosition = Vector3.zero;
    20.         addedItem.transform.localScale = Vector3.one;
    21.  
    22.         if (go.name.Contains ("Campsite"))
    23.         {
    24.             addedItem.GetComponentInChildren<Text> ().text = ld [0].landmarkName;
    25.         }
    26.         else if(go.name.Contains("TwinPeaks"))
    27.         {
    28.             addedItem.GetComponentInChildren<Text> ().text = ld [1].landmarkName;
    29.  
    30.         }
    31.         else if(go.name.Contains("FallenOne"))
    32.         {
    33.             addedItem.GetComponentInChildren<Text> ().text = ld [2].landmarkName;
    34.  
    35.         }
    36.         else if(go.name.Contains("Obilisks"))
    37.         {
    38.             addedItem.GetComponentInChildren<Text> ().text = ld [3].landmarkName;
    39.  
    40.         }
    41.         else if(go.name.Contains("Mine"))
    42.         {
    43.             addedItem.GetComponentInChildren<Text> ().text = ld [4].landmarkName;
    44.  
    45.         }
    46.     }
    47.  
     
  2. spearbeard

    spearbeard

    Joined:
    May 6, 2018
    Posts:
    24
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LandmarkVisited : MonoBehaviour
    6. {
    7.  
    8.     public LandmarkController lc;
    9.  
    10.     // Use this for initialization
    11.     void Start ()
    12.     {
    13.         GameObject lcgo = GameObject.Find ("LandmarkController");
    14.         lc = lcgo.GetComponent<LandmarkController> ();
    15.     }
    16.    
    17.     // Update is called once per frame
    18.     void OnTriggerEnter (Collider col)
    19.     {
    20.         lc.AddItemToList (gameObject);  
    21.     }
    22. }
    23.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class LandmarkCollider : MonoBehaviour
    6. {
    7.  
    8.     public int scoreValue = 100;
    9.     private AudioSource source;
    10.     [SerializeField] private AudioClip clip;
    11.  
    12.     // Use this for initialization
    13.     void Start () {
    14.        
    15.     }
    16.    
    17.     void OnTrigger(Collider col)
    18.     {
    19.         Debug.Log ("You've visited the" + gameObject.name);
    20.         source = col.GetComponent<AudioSource> ();
    21.         source.PlayOneShot (clip);
    22.         ScoreController.score += scoreValue;
    23.         Destroy (gameObject);
    24.     }
    25. }
    26.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [System.Serializable]
    6. public class LandmarksData
    7. {
    8.     public string landmarkName;
    9.  
    10. }
    11.