New to coding, need help

Hi! this is my code currently and I’m having an issue,
Basically I don’t want it to be " if (Input.GetMouseButtonDown(1)) "
I want it to be OnTriggerEnter and I’ve tried everything I can think of to fix it!
(this code is from a tutorial)

Please let me know how to fix it

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

public class LevelLoader : MonoBehaviour
{
    public Animator transition;

    public float transitionTime = 1f;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            LoadNextLevel();
        }
    }

    public void LoadNextLevel()
    {
        StartCoroutine(LoadLevel(SceneManager.GetActiveScene().buildIndex + 1));
    }


    IEnumerator LoadLevel(int LevelIndex)
    {
        transition.SetTrigger("Start");

        yield return new WaitForSeconds(transitionTime);

        SceneManager.LoadScene(LevelIndex);
    }

}
1 Like
  1. Please define your task in one line.
  2. Which tutorial you use?
  3. If you want to load the next level by clicking the button, then use the Button instead of monitoring the Input states.

Hey, so i’m just trying to change scenes when i hit a trigger. but while changing scenes i want a animation to play,

the tutorial is this one: https://youtu.be/CE9VOZivb3I?si=S1FXuAcpzmgnn4Kf

I don’t want it to change when i press a button. i want it to change when i go into a trigger

What does it mean?

Well, you don’t want a button, so I guess it’s when your character enters the trigger.

In this case, you don’t need to use Input.GetMouseButtonDown.

Just define Unity - Scripting API: MonoBehaviour.OnTriggerEnter(Collider) instead of Update.

Correct me if I am wrong, or describe your task in more detail.

I’m not sure if your wrong, but I’ll try to go more in depth.
When i walk into a trigger i would like it to switch scenes but while that happens I want a animation to play.

So i’ve used the OnTriggerEnter command and it doesnt work, it gives me a ton of errors. Do you know any other fixes?

1 Like
  1. How did you use it?
  2. What errors?

I Tried. else, if, void, public void, private void, [MonoBehaviour.OnTriggerEnter(Collider), and all other variations I could think of. I started coding like a week ago so I’m probably just dumb.

about the errors, it was a ton of them and I kept fixing them and then new ones would be created.

1 Like

I need code examples and error examples.

Just use it in your class as it shown here: Unity - Scripting API: MonoBehaviour.OnTriggerEnter(Collider) as I said before. Don’t call — just define. And then make all needed operations inside the function.

You probably need to start with Unity Learn.

 void Update()
    {
        if (Input.GetMouseButtonDown(1))
        {
            LoadNextLevel();
        }
    }

i just wanna change the command " if (input.getmousebuttondown(1)) "
to OnTriggerEnter, im wondering what to put before it.

1 Like
private void OnTriggerEnter(Collider other)
{
    if (other.tag == "Player") // use correct tag here
    {
        LoadNextLevel();
    }
}

Thanks andrey, this helped alot.

1 Like