- Home /
The question is answered, right answer was accepted
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" which i know means that it thinks 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
//This 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 this 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 something 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.
Yeah thats what I didn't know about, after I posted this I figured out that I had to assign them in the inspector. I am so used to coding and running the final product. I have never had to link the codes together in a inspector before. I posted my own answer and selected it if any other noobs are not used to using Unity and the game objects/inspector.
Answer by BeerNuts · Dec 29, 2012 at 05:54 PM
Ok I figured it out exactly after I posted this which drives me nuts since I was trying to figure this out for hours yesterday.
So if anyone runs into this 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 hierarchy 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.
Follow this Question
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
Accessing Variables within another gameobject's script 2 Answers
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How do you reference a GameObject in a C# script that is a part of that GameObject? 1 Answer