Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by MadmanSilver · Dec 18, 2014 at 01:29 AM · nullreferenceexceptionfunctioncallinganother script

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image MadmanSilver · Dec 18, 2014 at 02:42 AM 0
Share

Yes, I have already tested that. Also, thanks for letting me know about that "Action" thing.

avatar image MadmanSilver · Dec 18, 2014 at 02:53 AM 0
Share

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

avatar image MadmanSilver · Dec 18, 2014 at 02:55 AM 0
Share

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'.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Calling a Function from other script ( C# ) 1 Answer

NullReferenceException: Object reference not set to an instance of an object ..... 1 Answer

How to call a function from another script without referencing it? 1 Answer

C# - How do I increase the variable from another script? 2 Answers

How to turn on and turn off function with button. 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges