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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Need help with destroying object C#

Discussion in 'Scripting' started by Space-Octopus, Sep 4, 2017.

  1. Space-Octopus

    Space-Octopus

    Joined:
    Jan 2, 2016
    Posts:
    7
    So my game is about getting to the exit before a wall of spikes kills you. My problem is that the wall won't disappear when a player gets killed by something that's not the wall. There's a video to show whats happening. What is supposed to happen is when the player hits the wall the player will die and so will the wall. The wall will respawn and so will the wall to there original positions. Working fine. Its just I want the wall to disappear along with the player when the player gets killed by something that is not the wall. But when that happens the whole thing starts messing up. I have no idea what to do by this point.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. [RequireComponent(typeof(AudioSource))]
    6. public class KillScript : MonoBehaviour {
    7.     public AudioClip deathSound;
    8.     AudioSource _Audio;
    9.  
    10.     public LevelManager levelManager;
    11.  
    12.     private GameObject clone;
    13.  
    14.     void Start () {
    15.         levelManager = FindObjectOfType<LevelManager>();
    16.         _Audio = GetComponent<AudioSource>();
    17.  
    18.         clone = GameObject.Find("Wall Composite(Clone)");
    19.     }
    20.  
    21.     void OnTriggerEnter2D(Collider2D other) {
    22.         if (other.name == "Player") {
    23.             levelManager.RespawnPlayer();
    24.             _Audio.PlayOneShot(deathSound, 1.00f);
    25.             Destroy(clone);
    26.         }
    27.     }
    28. }
     
  2. luisch125

    luisch125

    Joined:
    Jul 10, 2015
    Posts:
    35
    Hi. First, and unrelated with your question, if your levelManager is public, you don't have to look for it on Start(). Or you can make it private and look for it on Start() (whatever you need).

    One quick solution that comes to me is that instead destroying the wall, you could reposition it to its original position. If you do this, you will save resources and will avoid getting new wall clones.

    So, just add a Vector3 reference to the starting wall position (before Start() add a Vector3 cloneStartingPosition; and inside Start() add cloneStartingPosition=clone.transform.position; and change the Destroy(clone) line with clone.transform.position=cloneStartingPosition (or whatever name you gave to the Vector3 reference).

    I'm still a beginner myself, but I think that solution should work...

    Edit: of course, if you do that, you should not respawn the wall (I can't see the script where the wall is respawned, but you should delete the respawn wall line wherever it is).
     
  3. mafiadude1

    mafiadude1

    Joined:
    Jun 28, 2012
    Posts:
    59
    Is KillScript also attached to the spikes on the ground?
     
  4. Space-Octopus

    Space-Octopus

    Joined:
    Jan 2, 2016
    Posts:
    7
    The 'KillScript' is attached to the spikes.

    Could you elaborate please?
     
  5. luisch125

    luisch125

    Joined:
    Jul 10, 2015
    Posts:
    35
    I think my explanation was clear...

    Code (CSharp):
    1.     using System.Collections;
    2.     using System.Collections.Generic;
    3.     using UnityEngine;
    4.    
    5.     [RequireComponent(typeof(AudioSource))]
    6.     public class KillScript : MonoBehaviour {
    7.         public AudioClip deathSound;
    8.         AudioSource _Audio;
    9.    
    10.         public LevelManager levelManager;
    11.    
    12.         private GameObject clone;
    13.  
    14.         Vector3 cloneStartingPosition; // this is the Vector3 where we will store the wall starting position
    15.    
    16.         void Start () {
    17.             levelManager = FindObjectOfType<LevelManager>(); //as this is a public object, you don't need this line
    18.             _Audio = GetComponent<AudioSource>();
    19.    
    20.             clone = GameObject.Find("Wall Composite(Clone)");
    21.  
    22.             cloneStartingPosition=clone.transform.position; // before the wall starts moving, we store the initial position
    23.         }
    24.    
    25.         void OnTriggerEnter2D(Collider2D other) {
    26.             if (other.name == "Player") {
    27.                 levelManager.RespawnPlayer();
    28.                 _Audio.PlayOneShot(deathSound, 1.00f);
    29.                 clone.transform.position=cloneStartingPosition; //instead of destroying the wall, we move it to the initial position
    30.             }
    31.         }
    32.     }
    33.  
     
  6. Space-Octopus

    Space-Octopus

    Joined:
    Jan 2, 2016
    Posts:
    7
    I tried this but now I'm getting a NullReferenceException.
    NullReferenceException: Object reference not set to an instance of an object
    KillScript.Start() (at Assets/Scripts/Misc Scripts/KillScripts.cs:22)
     
  7. luisch125

    luisch125

    Joined:
    Jul 10, 2015
    Posts:
    35
    Then the wall is instantiated later than this script execution. I'm no expert, but I think you should put the vector3 on the wall script and call it on the killscript modifying this line: clone.transform.position=cloneStartingPosition; calling the cloneStartingPosition from the wall script.

    It seems you are quite more beginner than I am :) (no offense).
     
  8. Space-Octopus

    Space-Octopus

    Joined:
    Jan 2, 2016
    Posts:
    7
    No offense taken. After much trial and error I finally got it working. Much thanks to you of course for guiding me.
     
  9. luisch125

    luisch125

    Joined:
    Jul 10, 2015
    Posts:
    35
    how did you finally do it?