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
0
Question by Kprasad · Oct 21, 2011 at 03:54 PM · errornullattachnull reference

Null Reference exception when calling using "script.gameObject.GetComponent

I'm trying to access the values per se, of a game object that my script is not attached to. I used the script.gameObject.GetComponent() line but it still gives me the same null reference.

Any advice?

Comment
Add comment · Show 3
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 Kprasad · Oct 21, 2011 at 03:03 PM 0
Share

any Advice?

avatar image kevork · Oct 21, 2011 at 06:09 PM 1
Share

I'd advise you to post the script in question.

Additionally, your description is not very clear. If the script is not attached to the game object you want to access, why would script.gameObject access the non-attached game object in question?

avatar image Kprasad · Oct 21, 2011 at 07:34 PM 0
Share

I'm trying to access a script from a different game object I'm unsure about how to go about that

1 Reply

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

Answer by aldonaletto · Oct 21, 2011 at 07:17 PM

To access a script in another GameObject you need a reference to the object. You can assign this reference to a variable at the Inspector, or find it using tag or name, or get it in some functions like OnTriggerEnter, OnCollisionEnter, Raycast and others that return the transform, the collider or any component of the desired GameObject.
To set the reference at the Inspector:

 var target: GameObject; // drag the object here in the Inspector

To find it by name:

 var target = GameObject.Find("TargetObject");

Or by tag:

 var target = GameObject.FindWithTag("Enemy");

Anyway, once you have the reference to the object in some variable, you can access the script using a variable with the script type - which is the name of the script without quotes or extension. If the script is called EnemyControl.js, for instance, you can get a reference to the script using something like this:

 var enemyScript: EnemyControl; // declare a variable of the script type
 // get the script reference with the object reference and GetComponent
 enemyScript = target.GetComponent(EnemyControl); 
 if (enemyScript){ // check if the object actually has a EnemyControl script
     enemyScript.health = 100; // read or assign variables this way
     enemyScript.WakeUp(); // or call a EnemyControl function this way
 }
Comment
Add comment · Show 1 · 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 thekingofclubs · Aug 31, 2014 at 05:10 PM 0
Share

I am trying to access the script of enemy just the way you have described. But I am getting Null Reference Exception. I dont get the error always, only sometimes which is making it hard to debug. I cannot drag the enemy object in the inspector because the enemy object is an instantiated prefab. I find the enemy object using GameObject.FindWithTag("Enemy"). From the line number given in error, it looks like the player finds the enemy object but it cannot access the script. Why is this happening?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Null Reference Exception Error. Modified Stats. Need Help. 1 Answer

Null Reference Annoying Error 0 Answers

NullReferenceException - With Network PlayerSpawn 0 Answers

NullReferenceException yet Debug.Log shows nothing is null? 1 Answer

String in array is null when checking length, help bug?! 2 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