Home > front end >  Unity 2D OnTriggerEnter2D() will not repeat in an entirely different script
Unity 2D OnTriggerEnter2D() will not repeat in an entirely different script

Time:07-10

I am creating a game where there are 9 chunks in 1 2D map and you must "unlock" every chunk by triggering a collider on the edge in a specific order to beat the game. I have managed to switch the scene from 0 to 1 using this code on a rectangle with a trigger collider:

void OnTriggerStay2D(Collider2D other)
{
    SceneManager.LoadScene(1);
    Destroy(this.gameObject);
}

The code above works fine, but recently I created the third chunk and copied the code to a new script, changed the name, and switched SceneManager.LoadScene(1) to SceneManager.LoadScene(2), then added it to a new rectangle with a trigger collider. The second chunk loads in when triggered, but the third does not. Is there any way to fix this?

CodePudding user response:

have you added that scene to The scenes list in builds setting?

CodePudding user response:

I don't have enough reputation yet to comment, so I'll propose a few possible problems/solutions. I assume your chunks are scenes, so you're loading in several scenes at once? Some things I can think of that might be causing the second chunk not to load in:

  • Did you add the scene to the build? If not, you can do so in the build settings.
  • You can try to debug OnTriggerStay2D() in the second chunk trigger. Is it being triggered? If not, you might not have added the trigger to the rectangle correctly: did you set up the Rigidbody2D?

If the above points do not fix your issue, I'd suggest adding more of your code (of the chunk loading scripts) and some screenshots (of the inspector in both chunk loaders and of the colliders you made) to your question. Hope this helps.

  • Related