- Home /
Trouble accessing scripts (Boo)
Hello,
I'm currently having trouble using the GetComponent function to access scripts. I currently have three scripts. One called ForcesUmbrella attached to an object in the scene, another called CameraInfo attached to the Main Camera and the final one called GetInfo attached to both the camera and the game object.
What I am trying to do is access the GetPosition() function in ForcesUmbrella from CameraInfo through GetInfo but I keep getting a "NullReferenceException: Object reference not set to an instance of an object" error so I must be doing something wrong.
If somebody can point me in the right direction I would be more than grateful.
Thank you and here are the scripts:
#### ForcesUmbrella ####
import UnityEngine
class ForcesUmbrella (MonoBehaviour):
def Start ():
pass
def Update ():
pass
def GetPosition():
Debug.Log('It works')
#####################
#### CameraInfo ####
import UnityEngine
class CameraInfo (MonoBehaviour):
public umbInfo as GetInfo
def Start ():
umbInfo = GetComponent[of GetInfo]()
def Update ():
umbInfo.GetUmbrella()
##################
#### GetInfo ####
import UnityEngine
class GetInfo (MonoBehaviour):
public umbInfo as ForcesUmbrella
def Start ():
umbInfo = GetComponent[of ForcesUmbrella]()
def Update ():
pass
def GetUmbrella():
umbInfo.GetPosition()
##############
Answer by skalev · Mar 13, 2012 at 12:15 AM
I don't use boo, but it seems like your problem is that you are trying to get the component.
So, when you call GetComponent, you will only get the component attached to the current GameObject.
What you want to do is find the correct GameObject first:
http://unity3d.com/support/documentation/ScriptReference/GameObject.Find.html
and then get the script you need from that compnent using GetComponent.
Plus, this seems like a very inefficient way of doing things, why do you need 3 scripts to connect to one another?
Thank you skalev for the reply.
Indeed the problem was that I was not defining the correct GameObject before I searched for the script I wanted to access.
As for the efficiency I see what you mean. Going through the 'GetInfo' detour is unnecessary. Here I was just testing out accessing a GameObject's script from another GameObject. I thought that having a separate script 'GetInfo' that can be accessed by all objects to get other object's info would be good. But after sleeping on it I don't think so anymore.
Thanks again
Your answer
Follow this Question
Related Questions
GetComponent not Returning 1 Answer
gameObject.GetComponent("Script").enabled = true not working 5 Answers
C# GetComponent Issue 2 Answers