I don't know how to fix it. It's a 2D game. When you collide with the box, it should load/teleport you to the next scene, but it doesn't.
I tried everything on the internet and it didn't work.
using UnityEngine;
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour
{
public void LoadNextLevel()
{
void OnTriggerEnter2D(Collider2D collider)
{
SceneManager.LoadScene(1);
}
}
}
I expect it to teleport me to my next level.
It looks like you nested the OnTriggerEnter2D() function inside of LoadNextLevel()? That's what the Local Function warning is referring to.
Those should be two separate functions, not one within the other. OnTriggerEnter2D() is a function of MonoBehavior; the MonoBehavior (VictoryZone) is what gets notified of the collision.
using UnityEngine.SceneManagement;
public class VictoryZone : MonoBehaviour {
public void LoadNextLevel() {
SceneManager.LoadScene(1);
}
void OnTriggerEnter2D(Collider2D collider) {
LoadNextLevel();
}
}
Note: you might also need to check the GameObject associated with collider to make sure that it's a player, and not an enemy or something (if, hypothetically, you had enemies or projectiles or other objects with colliders moving into the victory zone).
Related
In my unity project, I have a GameObject called "Spawn_manager", that has another Child GameObject called "Enemy_Container". The script of Spawn_Manager makes "Enemy" prefabs instantiate inside the "Enemy_Container". I am trying to acess the "Enemy" script(also named Enemy) from another script.
Anyone knows how I can get it?
I tried writing this:
private Enemy _enemy;
private void Start()
{
_enemy = GameObject.Find("Enemy").GetComponent<Enemy>();
}
But obviously it is not working, but I can't get it to work. Basically, I wanna the script, and the path would be Spawn_Manager>Enemy_Container>Enemy.
If you want to make the Enemy easily accessible you can add all spawned Enemy to static list. Like this:
public class Enemy : MonoBehaviour
{
public static List<Enemy> AllEnemy { get; private set; } = new List<Enemy>();
private void Awake()
{
AllEnemy.Add(this);
}
private void OnDestroy()
{
AllEnemy.Remove(this);
}
}
Using Find is demanding and not a reliable operation. If you will have any other GameObjects with name Enemy it will found them instead of desired one.
in this case i think that problem is that after instantiation prefab new GameObject have name in format $"{prefab.name}(Clone)".
using UnityEngine;
public class PlayerCollision : MonoBehaviour
{
void onCollisionEnter (){
Debug.Log("We hit something.");
}
}
I have rigidbody on every object and I attacked the script to my object, but still not working. I am following a Brackeys tutorial and it is a little old so I don't know if something has changed about the syntax.
void OnCollisionEnter(Collision collision) {
}
I'm working on a 2D Pixel Platformer RPG, I need to develop a save and load mechanism in it. There are several scenes in the game (and will have many more), the question is, how do I save the scene number the player is currently in, so that when he quits and reloads the game, he's in the same scene. How can I implement it in C# Unity. (please be clear as I'm somewhat a beginner).
Ok, so there are a few things you need to do in order to achive this:
First, in the first scene in your build - create an Empty GameObject, name it "SceneManager".
Then, create a new tag "SceneManager" and add it to the "SceneManager" GameObject
Finally, add the "SceneManager" script to the "SceneManager" GameObject:
using System.Collections;
using UnityEngine;
using UnityEngine.SceneManagement;
public class SceneManager : MonoBehaviour
{
void Awake()
{
DontDestroyOnLoad(gameObject);
}
public void SaveScene()
{
int activeScene = SceneManager.GetActiveScene().buildIndex;
PlayerPrefs.SetInt("ActiveScene", activeScene);
}
public void LoadScene()
{
int activeScene = PlayerPrefs.GetInt("ActiveScene");
SceneManager.LoadScene(activeScene);
}
}
Then, you can load/save scenes by using this script:
using UnityEngine;
public class UsageScript: MonoBehaviour {
private SceneManager SceneManager;
void Awake ()
{
sceneManager = GameObject.FindGameObjectWithTag("SceneManager").GetComponent<SceneManager>();
}
void UsageManager()
{
sceneManager.SaveScene();
sceneManager.LoadScene();
}
}
I'm a beginner and I want to know how to make player stops movement when hitting an obstacle.
I tried this code but it shows that it's deprecated
public class Collision : MonoBehaviour
{
public PlayerMove move;
void onCollisionEnter (Collision info)
{
if (info.Collider.tag == "Obstacle")
{
move.enabled = false;
}
}
}
Be careful with methods in Unity, they start with an Uppercase.
OnCollisionEnter should start with an Uppercase to be recognized by Unity as the method called once per collision.
As scopolamin said, don't call your class Collision : you'd better rename it as well as your file, your class name and your file name must match in order to inherit from a MonoBehaviour.
I assume PlayerMove is the script you use as a character controller. Be sure your variable move has the right script attached.
You named your class Collision. The parameter info in the OnCollisionEnter method doesn't use UnityEngine.Collision but your own Collision class.
Change your class name Collision to something else:
public class Collision : MonoBehaviour
public class MyCollision : MonoBehaviour
I have a scene with two cubes, and canvas. One cube falls onto another. Canvas appearing must be triggered by collision. It's does not working. Where's the problem?
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class EverythingInside : MonoBehaviour
{
public Canvas GUICanvas;
void Start()
{
}
void OnGUI()
{
GUICanvas.gameObject.SetActive(true);
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.tag == "FallingCube")
{
OnGUI();
}
}
void Update()
{
}
}
Put the code below inside your EverythingInside script. Make sure Collider and Rigidbody are attached to both cubes. Also make sure to drag your Canvas from the Editor to the GUICanvas slot.
public Canvas GUICanvas;
void Start()
{
}
void OnCollisionEnter(Collision col)
{
if (col.gameObject.CompareTag("FallingCube"))
{
GUICanvas.gameObject.SetActive(true);
}
}
void Update()
{
}
Unity Physics tutorials.
Unity UI Tutorials.
Unity Scripting Tutorials.
Your problem is that OnGUI() as a function is called once every frame in Unity, regardless of whether or not you call it manually. Also, OnGUI is reserved for doing things like rendering simple buttons and text directly to the screen, it doesn't perform any actual logic.
All you have to do to make this work is just replace where you've called OnGui() with GUICanvas.gameObject.SetActive(true)
and get rid of the other functions.
using UnityEngine;
using UnityEngine.UI;
public class EverythingInside : MonoBehaviour
{
public Canvas GUICanvas;
void OnCollisionEnter(Collision col)
{
if (col.tag == "FallingCube")
{
GUICanvas.gameObject.SetActive(true);
}
}
}
That right there is literally all you need to make it work. Bear in mind that if you change it later on to be a trigger instead of two colliding cubes you will need to replave OnCollisionEnter with OnTriggerEnter.