- Home /
"The script needs to derive from Monobehavior","The script needs to derive from Monobehaviour"
I'm pretty new to both C# and Unity so I'm having a little difficulty with this code. Every time I try to add my script to an object I keep getting the "script needs to derive from monobehaviour" error. The class name and file names are the same also the Monobehavior line is added after the class name. Any help would be appreciated!
Here is the code:
using UnityEngine;
using System.Collections;
public class Touch : MonoBehaviour {
private Controls player;
void Start()
{
player = FindObjectOfType<Controls>();
}
public void LeftArrow()
{
player.moveright = false;
player.moveleft = true;
}
public void RightArrow()
{
player.moveright = true;
player.moveleft = false;
}
public void ReleaseLeftArrow()
{
player.moveleft = false;
}
public void ReleaseRightArrow()
{
player.moveright = false;
}
} ,I'm pretty new to both C# and Unity but when I try to add my script to an object I keep getting the "script needs to derive from monobehaviour" error. The class and the file are named the same thing and I have the Monobehavior line directly linked after the class name.
Here's the code:
using UnityEngine; using System.Collections;
public class Touch : MonoBehaviour
{ private Controls player;
void Start()
{
player = FindObjectOfType<Controls>();
}
public void LeftArrow()
{
player.moveright = false;
player.moveleft = true;
}
public void RightArrow()
{
player.moveright = true;
player.moveleft = false;
}
public void ReleaseLeftArrow()
{
player.moveleft = false;
}
public void ReleaseRightArrow()
{
player.moveright = false;
}
}
Have you tried to:
Copy the content of the script
delete the file
recreate the file
paste the code into the new file
Yes, I did that. I even tried to attach a new script directly to the object and copy the code over to it with the same error.
Answer by MonkeyHood · Jun 24, 2018 at 03:30 AM
Touch
is already the name of a struct in Unity. You need to name it something else.
Your answer
Follow this Question
Related Questions
"Cant't add Script behavior CallbackExecutor. The script needs to derive from Monobehavior" Tutorial 0 Answers
First Person Controller Script not working 3 Answers
Enabling multiple Monobehaviour Components in a game object 1 Answer
how to jump to a specific time in game 2 Answers
Camera Lookat Cursor Tutorial? 3 Answers