So I'm working my way throught the [2D Rogue-like Unity tutorial][1]. When I try to add a script to one of my game object I get the: *"Can't add Script behaviour. The script needs to derive from monobehaviour! "* error.
The object is GamerManager, and I tried to add a GameManager.cs script, as per the video.
I'm using MonoBehaviour, it's in the script. I followed the tutorial to the letter. I even tried using the code from the Unity web page and I keep getting the same message.
[Here's Unity official code:][2]
Cheers!
using UnityEngine;
using System.Collections;
using System.Collections.Generic; //Allows us to use Lists.
public class GameManager : MonoBehaviour
{
public static GameManager instance = null; //Static instance of GameManager which allows it to be accessed by any other script.
private BoardManager boardScript; //Store a reference to our BoardManager which will set up the level.
private int level = 3; //Current level number, expressed in game as "Day 1".
//Awake is always called before any Start functions
void Awake()
{
//Check if instance already exists
if (instance == null)
//if not, set instance to this
instance = this;
//If instance already exists and it's not this:
else if (instance != this)
//Then destroy this. This enforces our singleton pattern, meaning there can only ever be one instance of a GameManager.
Destroy(gameObject);
//Sets this to not be destroyed when reloading scene
DontDestroyOnLoad(gameObject);
//Get a component reference to the attached BoardManager script
boardScript = GetComponent();
//Call the InitGame function to initialize the first level
InitGame();
}
//Initializes the game for each level.
void InitGame()
{
//Call the SetupScene function of the BoardManager script, pass it current level number.
boardScript.SetupScene(level);
}
//Update is called every frame.
void Update()
{
}
[1]: https://unity3d.com/learn/tutorials/s/2d-roguelike-tutorial
[2]: https://unity3d.com/learn/tutorials/projects/2d-roguelike-tutorial/writing-game-manager?playlist=17150
↧