• 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 /
This question was closed Dec 29, 2012 at 09:23 PM by Fattie for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by BeerNuts · Dec 29, 2012 at 04:44 PM · gameobjectscriptingbasicsreferencemethod

Call Functions Across Scripts, Null Object Error

I am a total noob with unity and c# or please forgive me...

Ive got two assest I bought and I and trying to get them to talk to each other. 1) a sample completed game 2) a new ui

The error I am getting it "NullReferenceException: Object reference not set to an instance of an object" w$$anonymous$$ch i know means that it t$$anonymous$$nks I am calling a null.

Below is the UI Code trying to call Restart(); from LevelManager.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [ExecuteInEditMode()] 
 
 public class PauseManager : MonoBehaviour {
 
 public LevelManager levelManager;
 public PlayerManager playerManager;
 public GUIManager guiManager;
 public AudioManager audioManager;
 public CameraManager cameraManager;
 // Lots of other code between... //
 
 //Draw replay button
 if(GUI.Button(new Rect(maxButtonRight  + Screen.width/8 - scaleWidth/2,Screen.height*4/10.0f,scaleWidth,scaleWidth),textures[1],"trans_button"))
             {
                 isShow = false;
                 //------------------------------
                 //Do replay function in here
                 //-----------------------------
                 levelManager.Restart();
             }
 
 // Lots of other code between... //

Below is the Restart(); method in LevelManager.cs

 //T$$anonymous$$s is the main function for the level restart mechanic
     public void Restart()
     {
         if (inRestart)
             return;
         
         inRestart = true;
         //timeManager.Restart();
         guiManager.Restart();
         cameraManager.Restart();
         
         gatheredCoins = 0;
         
         for (int i = 0; i < coins.Length; i++)
             coins[i].SetActive(true);
             
         for (int j = 0; j < lifts.Length; j++)
             lifts[j].Reset();
             
         for (int k = 0; k < doors.Length; k++)
             doors[k].Restart();
             
         /* for (int n = 0; n < timeModifiers.Length; n++)
             timeModifiers[n].SetActive(true); */
         
         coinsToDisplay = "00/";
         
         if (coins.Length < 10)
             coinsToDisplay += "0" + coins.Length;
         else
             coinsToDisplay += coins.Length;
             
         guiManager.coins = coinsToDisplay;
         
         playerManager.Respawn();
         
         StartCoroutine(RestartOver());
     }

According to these links t$$anonymous$$s logic should work. http://answers.unity3d.com/questions/7555/how-do-i-call-a-function-in-another-gameobjects-sc.html http://docs.unity3d.com/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html

What am I doing wrong here? There is somet$$anonymous$$ng that I am missing. Because its not just an error with the LevelManager.cs but the new ui script also bugs on any reference to another script. So audioManager.PlayAudio(6); and cameraManager.DisableOrbit(); both give the same Null object error if I put them into the new ui script.

Comment
Add comment · Show 2
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 whydoidoit · Dec 29, 2012 at 04:45 PM 0
Share
avatar image BeerNuts · Dec 29, 2012 at 05:37 PM 0
Share

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by BeerNuts · Dec 29, 2012 at 05:54 PM

Ok I figured it out exactly after I posted t$$anonymous$$s w$$anonymous$$ch drives me nuts since I was trying to figure t$$anonymous$$s out for hours yesterday.

So if anyone runs into t$$anonymous$$s here is what you need to do. You can't just call the script in code like I was doing. You need to actually reference the object that has the script on it in Unity. So for the CameraManager what I did was in unity I selected the new scripts object. Then I draged the Camera object from the $$anonymous$$erarchy in the "Camera Manager" slot in the new script.

Basically you have to find the game object that has the script on it and drag that game object into the references that you gave. My camera had the camera script on it, my player model had the player script etc. So I had to drag each game object into the proper field in the unity inspector for the new script.

Comment
Add comment · 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

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to run function in another script with prefabs? C# 2 Answers

How do I call a function in another gameObject's script? 5 Answers

Referencing a Public GameObject from another script. 2 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Accessing Variables within another gameobject's script 2 Answers


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