OnMouseUp script error CS0116
Hello, I'm trying to create a simple script which when mouse is released, it will either quit the game or load a level. But I get the common "A namespace can only contain types and namespace declarations" I've searched through the code and I can't find what's wrong with it myself.
using UnityEngine;
using System.Collections;
var isQuitButton = false;
public class onmouseup : MonoBehaviour {
function OnMouseUp()
{
//is this a quit button
if(isQuitButton)
{
//quit game
Application.Quit();
}
else
{
//load level
Application.LoadLevel(1);
}
}
}
Thanks.
Answer by pekalicious · Nov 15, 2015 at 05:58 PM
This looks like C#, mixed with UnityScript code. Did you accidentally create a C# script instead of a Javascript script? If yes, then create a new Javascript script, copy paste everything and delete the using statements and the class definition and you should be fine.
If you chose C#, I see two issues:
A) isQuitButton is outside the class onmouseup and B) you do not define methods by using the "function" keyword. Also, it is recommended that your class name is PascalCase.
So your script should really be:
using UnityEngine;
using System.Collections;
public class OnMouseUp : MonoBehaviour {
public bool isQuitButton;
public void OnMouseUp() {
// your logic here
}
}
Answer by centelleo · Nov 15, 2015 at 07:16 PM
Hi thanks for responding Yes I did C# instead of Javascript, oops. It works fine now. Thankyou!
Your answer
![](https://koobas.hobune.stream/wayback/20220612045152im_/https://answers.unity.com/themes/thub/images/avi.jpg)