• 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 Noise crime · May 31, 2010 at 11:30 AM · getcomponentnull

Caching script reference via GetComponent has local scope?

I'm trying to cache a reference to another gameobject script in Start function. It works fine within the start function, but try to access the variable elsewhere and its become null.

using UnityEngine; using System.Collections;

public class test_cacheComponent : MonoBehaviour {

 public  GameObject          go_AppManager;
 private applicationManager  m_AppManagerScript;

 void Start ()
 {
     applicationManager m_AppManagerScript = (applicationManager) go_AppManager.GetComponent(typeof(applicationManager));
     print("Test>>Start: Load m_AppManager script " + m_AppManagerScript);
     m_AppManagerScript.TestApplicationManger();
 }

 void Update ()
 {       
     print("Test>>Start: Load m_AppManager script " + m_AppManagerScript);
     m_AppManagerScript.TestApplicationManger();
 }

}

When run this will correctly print and call 'TestApplicationManger' (function simply prints OK) in Start, but come Update and m_AppManagerScript appears to be void. How come? I thought that Start() was called after all objects had been initialised and clearly AppManger is as I can call its functions, but it appears that the reference is only valid in locally in scope to the start function?

In addition I tried to check the variable with this line in Update

    if (m_AppManagerScript == null)
    {
        print("NULL");
        applicationManager m_AppManagerScript = (applicationManager) go_AppManager.GetComponent(typeof(applicationManager));
    }

But that brings up the same error 3 times in the same script error CS0135: `m_AppManagerScript' conflicts with a declaration in a child block Which i don't understand since i'm not re-declaring the variable at all.

Cheers

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

1 Reply

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

Answer by Lucas Meijer 1 · May 31, 2010 at 11:37 AM

Your C# foo is a bit rusty:

You are declaring a field of type applicationManager, called m_AppManagerScript.

then in your Start() function, you think you are assigning a value to that field, but what you're doing is creating a local variable with the same name, and assign a value to that.

use this start function instead:

void Start ()
    {
       m_AppManagerScript = go_AppManager.GetComponent<applicationManager>();
        print("Test>>Start: Load m_AppManager script " + m_AppManagerScript);
        m_AppManagerScript.TestApplicationManger();
    }

Note that I changed the first line (and also made you use the generic GetComponent<>() instead.

Most c# programmers stick to a convention where typenames start with a capital letter by the way. Feel free to care or not care about that :)

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 Noise crime · May 31, 2010 at 11:59 AM 0
Share

Doh! So obvious now you point it out. Thats what you get for testing with a local variable then copy & pasting it ;)

Any difference between generic vs typeOf method? I came across both methods whilst reading up on Unity and C# and actually preferred the typeof method. Though i'm liking the shorter generic version now more.

avatar image Mike 3 · May 31, 2010 at 12:45 PM 0
Share

Generic version is the same behind the scenes, it just returns an object which doesn't need casting ins$$anonymous$$d of a Component

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.

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

No one has followed this question yet.

Related Questions

UnityEngine.GameObject.GetComponent[WeaponBehavior] 1 Answer

Use variable from another script with WWW give NULL? 1 Answer

GetComponent keeps returning null 3 Answers

Getting NullReference when calling Instantiate from another script 0 Answers

Get Component or Check if Null SoftLocks Game (C#) 1 Answer


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