- Home /
Calling a function in another script.
I have looked everywhere and cannot find a fix for this. I am trying to run a function that is in another script on my game object, but I keep getting: NullReferenceException: Object reference not set to an instance of an object GUIButton.OnGUI () (at Assets/Scripts/GUIButton.cs:29)
I have checked everything and cannot figure out why it is throwing this exception. Here is the script I am running: using UnityEngine; using System.Collections;
public class GUIButton : MonoBehaviour {
public Texture2D texture;
public GameObject guiObject;
private Camera cam;
private Rect button;
private GameObject cam1;
private GameObject cam2;
void Start() {
cam1 = GameObject.Find("1st Person");
cam2 = GameObject.Find("3rd Person");
cam = cam1.camera;
if (cam1.activeSelf == true) {
cam = cam1.camera;
} else if (cam2.activeSelf == true) {
cam = cam2.camera;
}
GUITexture gui = guiObject.GetComponent<GUITexture>();
button = gui.GetScreenRect(cam);
}
void OnGUI() {
if (GUI.Button(button, texture)) {
(guiObject.GetComponent("Action") as Action).Activate(guiObject);
}
}
}
And here is the script that contains the function I am accessing:
using UnityEngine;
using System.Collections;
public class Action : MonoBehaviour {
private GameObject bob;
private GameObject cam1;
private GameObject cam2;
void Start () {
bob = GameObject.Find("Bob");
cam1 = GameObject.Find("1st Person");
cam2 = GameObject.Find("3rd Person");
}
public void Activate(GameObject obj) {
if (obj.name == "Exit To Main Menu") {
Resume();
} else if (obj.name == "Options") {
OpenOptions();
} else if (obj.name == "Resume") {
MainMenu();
}
}
private void Resume() {
GameObject.Find("In-Game Menu").SetActive(false);
(bob.GetComponent("Character Motor") as CharacterMotor).enabled = true;
(bob.GetComponent("FPSInput Controller") as FPSInputController).enabled = true;
(bob.GetComponent("Mouse Look") as MouseLook).enabled = true;
(cam1.GetComponent("Mouse Look") as MouseLook).enabled = true;
(cam2.GetComponent("Mouse Look") as MouseLook).enabled = true;
}
private void OpenOptions() {
}
private void MainMenu() {
}
}
Any and all help is appreciated.
Answer by SkaredCreations · Dec 18, 2014 at 01:33 AM
Are you sure you have dragged the correct object containing the script Action in the inspector? You can make sure by declaring guiObject as Action instead of GameObject (so you won't even need GetComponent in OnGUI). Also "Action" is already a class name used in .NET so I'd rename your class in case the compiler is trying to convert .NET's Action into your own Action.
Yes, I have already tested that. Also, thanks for letting me know about that "Action" thing.
I have change the name of that script and the class inside it and got a different error:
Internal compiler error. See the console log for more information. output was:error CS0016: Could not write to file `Temp/Assembly-CSharp-firstpass.dll', cause: Sharing violation on path /Volumes/GLADOS/UnityProjects/GreatWorldOfPandas/Temp/Assembly-CSharp-firstpass.dll.mdb
I just reopened unity and it changed:
Internal compiler error. See the console log for more information. output was:BCE0011: An error occurred during the execution of the step 'Boo.Lang.Compiler.Steps.SaveAssembly': 'Sharing violation on path /Volumes/GLADOS/UnityProjects/GreatWorldOfPandas/Temp/Assembly-UnityScript-firstpass.dll'.