Hey guy’s!
I’ve been trying to get this script (showed below) to work, But it never works. i have been trying for hours upon hours to make it work but it doesn’t. I want my player to roll over a collider and it needs to go to the next scene (an ending scene). Could you guys leave any suggestions?
Ps
I have marked some of the errors by useing: <( ) At the end of a line
using UnityEngine;
using System.Collections;
using UnityEngine.SceneManagement;
public class NextScene : MonoBehaviour {
void OnTriggerEnter (Collider : other)
{
if (other.CompareTag(“Player”)) <(The name 'other’does not exist in the current context)
}
SceneManager.LoadScene(“3” LoadSceneMode.Additive); <(The name ‘sceneManager.LoadScene’ and LoadSceneMode.Additive does not exist in the current context)
}
Your method doesn’t look right. I think you’re combining c# and Unityscript
Also, your brackets are wrong. After your if, put an open bracket { and then do the closing bracket after your statements. }
void OnTriggerEnter(Collider other)
{
if()
{
//code to load scene
}
}
Don’t forget to have a closing bracket for the class as well.
And of course…use code tags Using code tags properly - Unity Engine - Unity Discussions
2 Likes
First thing ill try out in the morning thank you!
Hey brathnann,
Thanks for response! it has been keeping me busy all morning again. And i’ve come to the following script (Down below)
It only gives me one error… I have marked LoadSceneMode witch is giving me the following error: enum UnityEngine.SceneManagement.LoadSceneMode Used when loading a scene in a player. And sorry for being such a noobie.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MissionComplete3 : MonoBehaviour {
void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player")) {
SceneManager.LoadScene("3" LoadSceneMode.Additive);
}
}
}
hmm…I’ll be honest, the error isn’t something I recognize. The enum appears to be correct. Is this an error or just a warning/notice?
When does the error occur? Can you compile and play mode works? Or does it not run because of this notice?
Error building Player because scripts have compile errors in the editor. This is the error that shows up on my screen i have attached a rigidbody and a box collider and the cylider is set as an tirgger. I made a maze and when you exit the maze i want it to pop to an exit scene/screen. when i want to start the game it says: all compiler errorrs have to be fixed before you can enter playmode. the loadscenemode also says Xyntax error, ‘,’ expected… after the enum Unityengine.scenemanamgent.loadscenemode used when loading a scene in a player,
Oh, I see it now. You don’t have a comma between “3” and LoadSceneMode.Additive.
Don’t forget when calling a method with multiple parameters, you need to separate them with commas
1 Like
So, to continue this from what you messaged me…
MissionComplete3 script should be on a collider at the end of your level. This collider should be a trigger. Your player should have a collider(no trigger needed) and a rigidbody as well as be tagged “Player”.
If the scene isn’t loading, make sure you have a scene named “3” and it is added to your build list, or else it isn’t going to load it. I would also add a debug.log to that if statement to make sure it is triggering.
Note, if your level3 scene is named something else, that name must match and you will need to replace the 3 with the scene name.
Also, make sure you really want LoadSceneMode.Additive, and not just a normal scene load.
Oke, it should give a check box to check but it doesn’t. You are probably right i should have it set as a normal sceneload. But if i change it to LoadScene.Additive it gives an error. This is a new script so it’s named MissionComplete.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MissionComplete : MonoBehaviour
{
void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player")) {
SceneManager.LoadScene("even cooler - kopie", LoadScene.Additive);
Debug.Log("Even cooler - kopie));
}
}
}
So it’s still not working for you?
Is your scene named “even cooler - kopie” ? *Note that caps do matter! It is case sensitive
LoadScene.Additive gives an error because it is LoadSceneMode.Additive. That is the correct syntax. When I asked if you really wanted additive, it was because Additive adds the scene you are loading to the current scene, which may or may not be what you want.
Hey sorry i was on a quick vacation.
I’ve made some adjustments and changed the name of the scene i want to load. It’s not showing any error but still it does not work. The cylinder that’s the trigger has a rigidbody and a collider and is set as a trigger. and my the object that needs to cause the trigger is tagged as Player.
using UnityEngine;
using UnityEngine.SceneManagement;
public class MissionComplete : MonoBehaviour
{
void OnTriggerEnter(Collider other) {
if (other.CompareTag("Player"))
SceneManager.LoadScene("MissionComplete", LoadSceneMode.Additive);
}
}
If you put a debug statement in the ontriggerenter before the if statement and another within the if statement, do they both show in the console?
No they do not. Damn i feel like such a noob ^^
using System;
using System.Diagnostics;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MissionComplete : MonoBehaviour
{
void OnTriggerEnter(Collider other) {
Console.WriteLine("Mode=Debug");
if (other.CompareTag("Player"))
Console.WriteLine("Mode=Release");
SceneManager.LoadScene("MissionComplete", LoadSceneMode.Additive);
}
}
Interesting that you used Console.WriteLine instead of Debug.Log(“a string”) or even print(“a string”); Haven’t seen that. Might change it just You’ll want to change those to use either the Debug.Log or print so that it shows in the unity Console(which is important for play mode) makes it a little easier to see if it’s triggering while playing.
Now, if the messages don’t appear when you switch those, then I would make sure your player is touching the cylinder at some point. And just to make sure, your player does have a collider as well, correct? And that collider is entering the cylinder? (not just the player, as the collider has to trigger the event)
Before i continue, i need to tell you that i google everything i write down in the script. And since everything is super vage on google and complex i just try a bunch of shit till it doesn’t give me an error. And yes the Cylider has a collider and is set as trigger. and yes it player goes through the Cylinder compleetly. Oke i think the script works… Wierd when i set the player as a trigger in the collider it triggers the next scene? but it just fall through the playing field.
If your player has a trigger collider, then it doesn’t collide with the ground, which will make you fall through.
The other collider should be triggering things as well, unfortunately I can’t really give you much more info on why it doesn’t without seeing how your scene is setup.
And we all start somewhere. Google is a great tool. Console.WriteLine is used normally with console apps developed in visual studios or other programs. So I figure that is why you used it.
Yes i am useing microsoft visual studio. Should i use something els? I’m sorry to ask but i have no clue where to put the Debug log and what to write in the String. If my script is like this (showed down below) It gives me an error: A using directive can only be applied to namespacs but unityengine.debug denotes a type. consider using static instead.
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.Debug;
public class MissionComplete : MonoBehaviour
{
void OnTriggerEnter(Collider other) {
Debug.Log("Player");
if (other.CompareTag("Player"))
Debug.log("Player");
SceneManager.LoadScene("MissionComplete", LoadSceneMode.Additive);
}
}
No, sorry for the confusion. If I were developing an app straight in visual studios, I would use Console.WriteLine for a console app. Because you are using VS with Unity, you should use Debug.Log (there are also others like Debug.LogError) as this will print in the Unity console.
I find it intressting that when i removed Console.WriteLine(“Mode=Debug”) and kept Console.WriteLine(“Mode=Release”); In my script. And it was like everything was the trigger when i pressed Play it instantly went to my missioncomplete scene
Well i could not make it work… Massive Thanks to you for all the support and help! This is what i ended up with
using System;
using UnityEngine;
using UnityEngine.SceneManagement;
public class MissionComplete : MonoBehaviour
{
public void OnTriggerEnter(Collider other)
{
Debug.Log("Collider", gameObject);
if (other.CompareTag("Player"))
{
SceneManager.LoadScene("MissionComplete", LoadSceneMode.Single);
}
}
}