• 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 oliver-jones · Nov 27, 2010 at 03:07 PM · variablegetcomponentstaticmouseclick

GetComponent Keeps Returning : Null??

Hello,

I have 2 scripts in my game, one that changes a variable true or false depending if the user has clicked on the object which has the script attached to it. And another that has a GetComponent to read if that variable is true or false and executes an if statement.

ClickObject:

#pragma strict

var clicked : boolean = false; var hit : RaycastHit; static var ClickedOn : boolean = false; var ClickTurret = false;

function Update() { if(Input.GetMouseButtonDown(0) && collider.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), hit, Mathf.Infinity)) { clicked = !clicked; if(tag == "Select") tag = "Unselect"; else tag = "Select"; //Debug.Log("clicked" + (clicked? "" : " off")); } ClickedOn = clicked; ClickTurret = ClickedOn; }

Other Script:

function Update () { var clickObject : ClickObject; clickObject = GetComponent(ClickObject);

 if(clickObject.ClickTurret == true) //clickObject.ClickedOn
 { ......

The problem is, is that the GetComponent keeps returning null. I get a NullReferenceExeption in my console: Object reference not set to an instance of an object?

Please Help, Thanks!

PS: I'm trying to avoid static / global variables.

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

2 Replies

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

Answer by azzogat · Nov 27, 2010 at 03:57 PM

To reference a script on another object you could do something like this:

var myOtherScript: scriptName;

myOtherScript = gameObject.FindWithTag("myOtherObjectTag").GetComponent(scriptName) as scriptName;

You can access it like:

myOtherScript.function();

or

myOtherScript.someVariable = something;

You could also search for the gameobject by name but it's recommended to use the FindWithTag way.

Edit: this seems more appropiate for your needs:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentInChildren.html

Comment
Add comment · Show 6 · 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 oliver-jones · Nov 27, 2010 at 04:18 PM 0
Share

Sounds like a good idea - but where do I put the myOtherObjectTag - Do I create a new Tag in the Inspecter and apply it to my object that has the script? Because my object already has a tag on it that I need.

avatar image oliver-jones · Nov 27, 2010 at 04:30 PM 0
Share

I'm still getting a NullReferenceException.

This is what I have: var myOtherScript: ClickObject; myOtherScript = gameObject.FindWithTag("Unselect" || "Select").GetComponent(ClickObject) as ClickObject; if(myOtherScript.ClickTurret == true){ print("true"); }

avatar image azzogat · Nov 27, 2010 at 04:34 PM 0
Share

I think this should fit your needs more accurately:

http://unity3d.com/support/documentation/ScriptReference/GameObject.GetComponentInChildren.html

avatar image oliver-jones · Nov 27, 2010 at 04:42 PM 0
Share

<< That looks good - but how to I do the oposite, Returns component in parent?

avatar image azzogat · Nov 27, 2010 at 04:53 PM 0
Share

Unless you have a lot of other operations relying on the variable you could move your vars to the child and only use the getcomponentinchildren: Parent cube: on click > set childScript.clicked = true; Child cube: var clicked: boolean; if(clicked){ do;}

Unless I'm not understanding your structure, this should do it.

Show more comments
avatar image
0

Answer by Adam Rademacher · Nov 27, 2010 at 03:36 PM

The way you have it written, both scripts need to be on the same object. When you use GetComponent by itself, it only looks on the same game object, so you'll either need to find a reference to the game object with clickobject or use a static.

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 oliver-jones · Nov 27, 2010 at 03:45 PM 0
Share

How will I go about adding a reference. This is how my scene is laid out. I have a Cube with the click script on it. Then inside that cube (as a child) is another Cube, with the 'Other Script' attached to it.

avatar image Adam Rademacher · Nov 27, 2010 at 09:34 PM 0
Share

clickobject = transform.parent.GetComponent(ClickObject);

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

How to get Component from Game Object in C# 1 Answer

GUI.DrawTexture Error & Static Variables!! 2 Answers

C# script for calculating the distance between the player and objects 1 Answer

GetComponent C# 1 Answer

Having trouble with static references. 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