Search Unity

null code

Discussion in 'Scripting' started by cd9992107, Sep 28, 2019.

  1. cd9992107

    cd9992107

    Joined:
    Aug 9, 2019
    Posts:
    14
    After destroying my object, this error appears "MissingReferenceException: The object of type 'GameObject' has been destroyed but you are still trying to access it.
    our script should either check if it is null or you should not destroy the object.
    SpawnController.Spawn () (at Assets/SpawnController.cs:59)". meu código.
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class SpawnController : MonoBehaviour {
    6.  
    7.     public float maxHeight;    
    8.     public float minHeight;    
    9.  
    10.     public float rateSpawn;    
    11.     private float currentRateSpawn;
    12.  
    13.     public GameObject objectPrefab;    
    14.     public int maxSpawnObjects;    
    15.  
    16.     private List<GameObject> objectsList = new List<GameObject>();  
    17.  
    18.  
    19.    
    20.  
    21.  
    22.    
    23.  
    24. // Use this for initialization
    25. void Start () {
    26.  
    27.         for (int i = 0; i < maxSpawnObjects; i++) {    
    28.            
    29.             objectsList.Add(Instantiate(objectPrefab));    
    30.             objectsList[i].SetActive(false);    
    31.         }
    32.  
    33.  
    34.  
    35.  
    36.     }
    37. // Update is called once per frame
    38. void Update () {
    39.  
    40.         currentRateSpawn += Time.deltaTime;  
    41.         if (currentRateSpawn >= rateSpawn) {    
    42.  
    43.             currentRateSpawn = 0;
    44.             Spawn();  
    45.         }
    46.  
    47.  
    48.    
    49.  
    50. }
    51.  
    52.     private void Spawn() {    
    53.  
    54.         float randHeight = Random.RandomRange(maxHeight, minHeight);  
    55.  
    56.         for (int i = 0; i < maxSpawnObjects; i++) {    
    57.  
    58.             if (objectsList[i].activeSelf == false) {    
    59.                 objectsList[i].transform.position = new Vector3(transform.position.x, randHeight, 0);  
    60.                 objectsList[i].SetActive(true);    
    61.                 break;
    62.             }
    63.         }
    64.     }
    65. }
     
  2. DreamingInsanity

    DreamingInsanity

    Joined:
    Nov 5, 2017
    Posts:
    101
    Where do you destroy the object?
     
  3. cd9992107

    cd9992107

    Joined:
    Aug 9, 2019
    Posts:
    14
    I created a key event, after the animation finished it activates with this script.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Clique : MonoBehaviour
    6. {
    7.     public Animator anim;  
    8.     public KeyCode animação;
    9.     public Transform spell;
    10.    
    11.  
    12.  
    13.  
    14.     public void CallDestruction(){
    15.         Destroy(this.gameObject);
    16. }
    17.  
    18.  
    19.  
    20.  
    21.     void Start()
    22.  
    23.     {
    24.  
    25.  
    26.        
    27.     }
    28.  
    29.  
    30.  
    31.    
    32.  
    33.  
    34.     // Update is called once per frame
    35.     void Update()
    36.     {
    37.  
    38.    
    39.        
    40.     }
    41.  
    42.     void shootNow() {
    43.    
    44.     Instantiate (spell, new Vector3 (1, .75f, 0), spell.rotation);
    45. }
    46.  
    47.     void OnMouseDown() {
    48.  
    49.     anim.SetTrigger ("mudança");
    50. }
    51. }
    52.  
     
  4. Yoreki

    Yoreki

    Joined:
    Apr 10, 2019
    Posts:
    2,605
    You instantiate a list of X items, then iterate over them using this X, while the actual list size can seemingly change in the background - or rather the objects can be destroyed. Since you still keep them in the list, the compiler tells you that you are trying to access something you dont have anymore. So when you destroy something, but have it in some list, then you obviously need to remove it from the list as well before destroying it. And you need to iterate over the list by using list.Count, instead of maxSpawnObjects, since otherwise you iterate over removed indices, resulting in an IndexOutOfBoundsException.

    That said, i have a few notes on code quality:
    • You should absolutely never use language specific letters when coding, this can cause tons of different problems.
    • Generally speaking, you should write your variable names and so on in english. This makes it consistent with the APIs (and 99.9% of other code that exists) and makes it easier for people to understand, which is important especially when you post it online to get help.
    • Following common conventions, you will want to write variables in camelCase (which you do), but write names for methods, classes and properties in PascalCase ("upper camel case"), starting with a capital letter.
    • Comparing booleans to true or false is considered bad practice. If you have a bool called "isSomething" and you want to check if that's true, then ise if(isSomething), and if you want to check if it's false use if(!isSomething). You dont need, and should not, do something along the lines of isSomething == true.