-2

I am trying to make a check point, I want to make it so when the character hits the checkpoint, the collider turns off (I am open to using a raycast but that wasn't the current plan). I am still new to unity and I can't get the code to compile. I think my issue may be just not calling objects properly???

public class checkpoint : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        cp = GetComponent<Collider>();
        cp.isTrigger = true;
        object = GameObject.Find("Check Point");
    }

    private void OnTriggerEnter(cp)
    {
        cp.GetComponent(BoxCollider).isTrigger = false;
    }
}

Not sure what is wrong

1 Answers1

0

When you look at the code in your IDE, you will see red outlines underneath the exact points in your code that have issues. When you hover your cursor over these points the IDE will tell you what the issue is.

enter image description here

You can even use the Show potential fixes functionality and the IDE can often fix the issue for you automatically.

enter image description here

Here is the code modified so that it compiles.

public class checkpoint : MonoBehaviour
{
    Collider cp;

    // Start is called before the first frame update
    void Start()
    {
        cp = GetComponent<Collider>();
        cp.isTrigger = true;
        GameObject gameObject = GameObject.Find("Check Point");
    }

    private void OnTriggerEnter(Collider other)
    {
        cp.isTrigger = false;
    }
}

You were missing declarations for your cp and object variables. Additionally object is a reserved keyword in C#, so you can't use it as a variable name.

On the OnTriggerEnter function declaration the cp parameter was missing its type Collider.

In the body of the method the GetComponent call has incorrect syntax.

UPDATE: You can use the Destroy method to destroy the Collider component when something enters inside its bounds for the first time.

[RequireComponent(typeof(Collider))]
public class Checkpoint : MonoBehaviour
{
    void Reset()
    {
        var collider = GetComponent<Collider>();
        collider.isTrigger = true;
    }

    private void OnTriggerEnter(Collider other)
    {
        var collider = GetComponent<Collider>();
        Destroy(collider);
    }
}

If you want to destroy the whole GameObject that contains the checkpoint component and the Collider component you can use Destroy(gameObject);.

Sisus
  • 651
  • 4
  • 8
  • Awesome, this helped a lot! My only problem is this just turns of the "is trigger" and doesn't remove the collider or otherwise make it so I can move through the check point. Also I am struggling to get my IDE (VS Code) to download the libraries because I have only done it in python (this is my first time in c#) with terminal commands, so I'm not currently getting a lot of the IDE help I'd like. This should let me move forward though so thank you. – Alex Bope Aug 26 '22 at 05:44
  • @AlexBope you want [`cp.enabled = false;`](https://docs.unity3d.com/ScriptReference/Collider-enabled.html) instead of `isTrigger` ... if you change [`isTrigger`](https://docs.unity3d.com/ScriptReference/Collider-isTrigger.html) you only convert the collider from a trigger to a solid collider ... – derHugo Aug 26 '22 at 09:05
  • I highly recommend you invest the time to get your IDE working properly - it really pays off :) I recommend trying out [Visual Studio Community 2022](https://learn.microsoft.com/en-us/visualstudio/gamedev/unity/get-started/getting-started-with-visual-studio-tools-for-unity). – Sisus Aug 26 '22 at 17:08
  • I updated my answer to show how to destroy the Collider. Just disabling the Collider like @derHugo suggested would also work to stop the OnTriggerEnter function from firing again. – Sisus Aug 26 '22 at 17:16