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

Fix? The object of type 'GameObject' has been destroyed but you are still trying to access it. Your

Discussion in 'Scripting' started by Florentin16, Jan 15, 2020.

  1. Florentin16

    Florentin16

    Joined:
    Jan 15, 2020
    Posts:
    14
    When i play the game, my first platform is destroyed, the second its still here and no generate another platforms.


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class PlatformGenerator : MonoBehaviour
    7. {
    8. public GameObject thePlatform;
    9. public Transform generationPoint;
    10. public float distanceBetween;
    11.  
    12. private float platformWidth;
    13.  
    14. // Start is called before the first frame update
    15. void Start()
    16. {
    17. platformWidth = thePlatform.GetComponent<BoxCollider2D>().size.x;
    18. }
    19.  
    20. // Update is called once per frame
    21. void Update()
    22. {
    23.  
    24. if(transform.position.x < generationPoint.position.x)
    25.  
    26. {
    27. transform.position = new Vector3(transform.position.x + platformWidth + distanceBetween, transform.position.y, transform.position.z);
    28.  
    29. Instantiate (thePlatform, transform.position, transform.rotation);
    30. }
    31.  
    32. }
    33. }
    34.  


    and for destroy the platforms

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlatformDestroyer : MonoBehaviour
    6. {
    7. public GameObject platformDestructionPoint;
    8.  
    9. // Start is called before the first frame update
    10. void Start()
    11. {
    12. platformDestructionPoint = GameObject.Find ("PlatformDestructionPoint");
    13. }
    14.  
    15. // Update is called once per frame
    16. void Update()
    17. {
    18. if(transform.position.x < platformDestructionPoint.transform.position.x);
    19.  
    20. {
    21. Destroy (gameObject);
    22. }
    23. }
    24. }
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    What's assigned to your "thePlatform" variable? If you've used the actual platform object in the scene, then destroy that object, then it has nothing to make a clone of. Make the platform a prefab, and link up that prefab to that variable instead.
     
    BlueDragoness likes this.
  3. Florentin16

    Florentin16

    Joined:
    Jan 15, 2020
    Posts:
    14
    Thank you so much! Fixed!