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
1
Question by user-8694 (google) · Feb 15, 2011 at 02:20 AM · getcomponentexceptionunassignedreferenceexcept

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.

Comment
Add comment · Show 4
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 tertle · Feb 15, 2011 at 02:35 AM 0
Share

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.

avatar image user-8694 (google) · Feb 15, 2011 at 02:48 AM 0
Share

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.

avatar image tertle · Feb 15, 2011 at 02:51 AM 0
Share

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.

avatar image user-8694 (google) · Feb 15, 2011 at 02:59 AM 0
Share

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?

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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>();
Comment
Add comment · Show 2 · 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 user-8694 (google) · Feb 15, 2011 at 06:04 AM 0
Share

Thanks. I just need to put this under the LateUpdate() for my$$anonymous$$ap and it works!

avatar image Molix · Feb 15, 2011 at 03:33 PM 0
Share

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

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

No one has followed this question yet.

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


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