Search Unity

Stuck on getting the selected object to destroy itself when colliding with another

Discussion in 'Visual Scripting' started by Mr_Mack, Sep 7, 2020.

?

How to destroy an object via code.

  1. NullExecption Error

    0 vote(s)
    0.0%
  2. GameObjects not being destroyed.

    0 vote(s)
    0.0%
  1. Mr_Mack

    Mr_Mack

    Joined:
    Aug 26, 2020
    Posts:
    1
    I am unable to get the obstacle object I want to destroy itself whenever it touches the main character of the game. I am used what I have learned from the endless runner tutorial and built my own variant based around that. Each time I try any other work around, I keep on getting a NullException error and I get lost each time. The code is below this message. Any help will be greatly appreciated.
    Code (CSharp):
    1. [code=CSharp]using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.SceneManagement;
    5.  
    6. public class DogPlayerController : MonoBehaviour
    7. {
    8.     // variables responsible for Jumping in the game
    9.     public float jumpStrength = 10;
    10.     public float gravityForce;
    11.     public bool grounded = true;
    12.     private float dogHealthCounter = 5;
    13.     private float dogHealth = 100;
    14.     private float playerWater = 1;
    15.  
    16.     // Variables for the other componets for the dog
    17.     private Rigidbody rigidBody;
    18.     private Animator dogAnimation;
    19.     public ParticleSystem dogDustParticle;
    20.     public bool gameOver;
    21.     private GameObject _dogplayer = new GameObject();
    22.  
    23.     // Variables to stop the nonsense with the Nullexpection error
    24.     private GameObject hazard = new GameObject();
    25.     private Transform finder;
    26.     private GameObject jackson = new GameObject();
    27.  
    28.     // Variables to make the object for the ItemSpawner class
    29.     ItemSpawner item = new ItemSpawner();
    30.  
    31.     // Start is called before the first frame update
    32.     void Start()
    33.     {
    34.         rigidBody = GetComponent<Rigidbody>();
    35.         dogAnimation = GetComponent<Animator>();
    36.         Physics.gravity *= gravityForce;
    37.     }
    38.  
    39.     // Update is called once per frame
    40.     void Update()
    41.     {
    42.         // when you press the spacebar to jump
    43.         if(Input.GetKeyDown(KeyCode.Space) && grounded && !gameOver)
    44.         {
    45.             rigidBody.AddForce(Vector3.up * jumpStrength, ForceMode.Impulse);
    46.             dogAnimation.SetTrigger("Arm_Dog|Jumped");
    47.             grounded = false;
    48.             dogDustParticle.Stop();
    49.         }
    50.     }
    51.  
    52.     private void OnCollisionEnter(Collision collision)
    53.     {
    54.         if(collision.gameObject.CompareTag("Ground"))
    55.         {
    56.             grounded = true;
    57.             dogDustParticle.Play(); // Alaways call the play method when you are needing to use the items like dust particles
    58.         } else if(collision.gameObject.CompareTag("Obstacle"))
    59.         {
    60.             jackson = hazard.transform.Find("Obstacle").gameObject;
    61.             item.OnCollisionEnter(collision);
    62.             _dogplayer.GetComponent<DogHealth>().gameDamage(15);
    63.         } else if (collision.gameObject.CompareTag("Water"))
    64.         {
    65.             // code to refill health for the player
    66.             jackson = hazard.transform.Find("Water").gameObject;
    67.             _dogplayer.GetComponent<DogHealth>().healthIncrease(10);
    68.         } else if (collision.gameObject.CompareTag("Obstacle") && dogHealth == 0)
    69.         {
    70.             gameOver = true;
    71.             dogAnimation.SetBool("Arm_Dog|Sitting 0", true);
    72.             dogDustParticle.Stop();
    73.             SceneManager.LoadScene("Game Over", LoadSceneMode.Additive);
    74.         }
    75.     }
    76. }
    77.  
    78.  
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ItemSpawner : MonoBehaviour
    6. {
    7.     // Variables for the spawner
    8.     public GameObject prefab;
    9.     private Vector3 spawnPos = new Vector3(1409, 49, 426);
    10.     private float startUpDelay = 4;
    11.     private float repeatingRate = 15;
    12.     private DogPlayerController playerControllerScript;
    13.     private new Rigidbody rigidbody;
    14.  
    15.     // Start is called before the first frame update
    16.     void Start()
    17.     {
    18.         rigidbody = GetComponent<Rigidbody>();
    19.         playerControllerScript = GameObject.Find("DogPlayer").GetComponent<DogPlayerController>();
    20.         InvokeRepeating("SpawnObstacles", startUpDelay, repeatingRate);
    21.     }
    22.  
    23.     // Let the rigidbody take control and detect collisions.
    24.     void EnableRagdoll()
    25.     {
    26.         rigidbody.isKinematic = false;
    27.         rigidbody.detectCollisions = true;
    28.     }
    29.  
    30.     public ItemSpawner()
    31.     {
    32.  
    33.     }
    34.  
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.      
    39.     }
    40.  
    41.     void SpawnObstacles()
    42.     {
    43.         // Creates zero obstacles
    44.         if (playerControllerScript.gameOver == false)
    45.         {
    46.             Instantiate(prefab, spawnPos, prefab.transform.rotation); // sets things up at the spawn position
    47.         }
    48.  
    49.     }
    50.  
    51.     // Code to destroy this object upon collision
    52.     public void OnCollisionEnter(Collision collision)
    53.     {
    54.         if (collision.gameObject.CompareTag("DogPlayer"))
    55.         {
    56.             Destroy(gameObject);
    57.         } else if (collision.gameObject.CompareTag("HuskyDog"))
    58.         {
    59.             GameObject fences = collision.gameObject;
    60.             Destroy(gameObject);
    61.         }
    62.     }
    63. }
    [/code]