- Home /
How to access variable from another c# script file?
Hi,
I am a unity newbie, know this is really quite a common question, and I have tried to look at some of the examples in the Unity scripting reference and unityAnswers, but I seem to be missing something in my understanding. Please note that I would prefer to use non static method.
I have a C# script named mycam.cs this is going to be attached to the Main Camera, on run mode, and initialized a variable, target which is a Transform type on load.
I have another file named mymap.cs, and within this file, I need to access the target Transform from the mycam.cs file, and use it within my mymap.cs script.
I have seen examples like Referencing non static variables from another script? C#, but it does not seems to work on my script.
Here's my script so far.
MyCam:
public class MyCam: MonoBehaviour { public Transform target; public float targetHeight = 2.0f; public float distance = 3.0f;
void LateUpdate () { // Ignore if target is not defined if (!target) return; } }
MyMap:
public class MyMap: MonoBehaviour {
public Transform player;
void Start()
{
GameObject target = GameObject.Find("target");
MyCam myCam = target.GetComponent<MyCam>();
Debug.Log("distance: " + myCam.distance);
player = myCam.target;
}
}
But when I tried to run, I received errors:
UnassignedReferenceException: The variable player of 'MyMap' has not been assigned. You probably need to assign the player variable of the MyMap script in the inspector.
I understand that somehow my player is not initialized for mymap script, but the target in mycam script is.
Kindly advise. Thanks.
So the object you are trying to find is called "target"? Can you show us the code that assigns target in $$anonymous$$yCam? It'll need to be called before the Start() in $$anonymous$$y$$anonymous$$ap.
hi tertle, as this mycam file is not written by me, i searched the mycam, but i couldn't find where they included the initialization, and could only find that they do a check on the LateUpdate() within the mycam file.
That might be your problem. $$anonymous$$yCam target isn't initialized when you try to set it in $$anonymous$$y$$anonymous$$ap unless you're doing it in inspector, therefore it's never assigned.
However the myCam target did initialize when I run the program, and I did saw the value in the target within the inspector when running, but not the player in the $$anonymous$$y$$anonymous$$ap is still empty. Does it mean that the initialization of the myCam target is not within myCam itself?
Answer by Molix · Feb 15, 2011 at 05:16 AM
If the MyCam script is attached to the main camera, instead of:
GameObject target = GameObject.Find("target");
MyCam myCam = target.GetComponent<MyCam>();
do:
MyCam myCam = Camera.main.gameObject.GetComponent<MyCam>();
Or, if you don't want to use Camera.main, set a reference to the camera in the inspector, e.g. in MyMap
public Transform theCamera;
And then it would be:
MyCam myCam = theCamera.gameObject.GetComponent<MyCam>();
Thanks. I just need to put this under the LateUpdate() for my$$anonymous$$ap and it works!
Actually it can go in Start(), where you had it. You should avoid doing it in Update/LateUpdate since that is every frame, and once is sufficient, unless the camera is being created/destroyed after the scene starts (and even then you'd probably only want to do it when that occurs, not continuously).
Your answer
Follow this Question
Related Questions
Best pattern for activating something 1 Answer
GetComponent null reference C# 1 Answer
Help with error: UnassignedReferenceException 5 Answers
UnassignedReferenceException 1 Answer
Check if ParticleSystem exist 0 Answers