Why i cant call this simple method?(c#)
THIS IS THE CLASS:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
public class GameManager : MonoBehaviour {
public void NextLevel(int currentlevel)
{
if (currentlevel==1)
{
SceneManager.LoadScene("Scene 2");
}
}
}
THIS IS THE SCRIPT THAT CALL THE CLASS WITH THE METHOD:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Logro : MonoBehaviour
{
public GameManager gamemanager;
public int currentlevel;
private void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
gamemanager.SendMessage("NextLevel",currentlevel);
}
}
}
BUT WHEN THE "PLAYER" ENTER IN THE COLLIDER IT SHOWS A ERROR, WHY??
I THINK THAT IS NOT THE WAY TO CALL A METHOD, HOW CAN I CALL IT?
Answer by Hellium · Aug 18, 2017 at 09:27 AM
Why don't you simply call the method as follow ?
gamemanager.NextLevel(currentlevel);
@Hellium is right. You can simply call the method on the class instance.
Send$$anonymous$$essage is otherwise to be used on the GameObject, not a component.
i tried both way, but when i enter in the collider appear the nextmessage:
NullReferenceException: Object reference not set to an instance of an object Logro.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Logro.cs:14)
NullReferenceException: Object reference not set to an instance of an object Logro.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/Logro.cs:14)
You simply haven't specified the Game$$anonymous$$anager
variable in the inspector.
Your answer
Follow this Question
Related Questions
Use a class like a function 1 Answer
How to call a method from a class in a different script in c# 1 Answer
Method not being called from another script 1 Answer
Trying to Make an Interact Icon Appear 1 Answer
Destroy instatiate object on trigger enter / collision,destroy instantiate prefab on trigger enter 0 Answers