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. Dismiss Notice

Question I want to Destroy a Game Object with a Lightsword:

Discussion in 'VR' started by unity_1C8786C2DFDB8B73EDFC, Apr 7, 2023.

  1. unity_1C8786C2DFDB8B73EDFC

    unity_1C8786C2DFDB8B73EDFC

    Joined:
    Mar 19, 2023
    Posts:
    1
    I am currently programming a virtual reality game for a school project. I want to destroy a game object with a lightsaber. I programmed it, but somehow it doesn't work. Can someone help me?:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;

    public class Destroy : MonoBehaviour
    {
    private void OnTriggerEnter(Collider other)
    {
    if (other.gameObject.CompareTag("Playstation"))
    {
    Destroy(gameObject);
    }
    }
    }
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,245
    1. Use code tags if you post code!
    2. Put in debug logs inside of the function and if statement to see where the code goes. If nothing triggers, the object the script is on does not have a collider set as trigger, the sword does not have a collider, or the sword (or object, at least 1 of the 2) does not have a rigidbody.

    if it's similar to beat saber, there are youtube videos on how to make that
     
  3. smallg2023

    smallg2023

    Joined:
    Sep 2, 2018
    Posts:
    96
    Destroy is a function name, probably best to not name your class the same.
    otherwise this is not really a VR specific problem, i would just check the usual issues i.e. one object must have a rigid body and the collider is set to trigger mode and the other object is correctly tagged.