Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 RandomMeasure · Feb 02, 2010 at 08:06 PM · scriptingbasicsreference

Having an object reference itself

I am still fairly new to Unity and programming in general. I have been having trouble comparing information between objects and the object that I have attached a script to. I would like to access all of the information for an object if it has a particular script attached to it. Is there any specific way to have an object reference itself or is there a programming technique I should use to make sure an object holds onto that information?

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
3
Best Answer

Answer by Ashkan_gc · Feb 02, 2010 at 08:30 PM

you can get a reference to the gameobject running the script itself by

this.gameObject

to see if a GameObject has script x attached you should try to use GetComponent and it will retun null if there is no component of type x attached.

if (GetComponent(x) == null)
{
//no component of type x attached
]
else
{
//component is attached
}
Comment
Add comment · Show 3 · 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 RandomMeasure · Feb 03, 2010 at 05:52 PM 0
Share

Thanks! this.gameObject was exactly what I was look for. I assume "this" can be used to reference other aspects of the object that the script is attached to.

avatar image burnumd · Feb 25, 2010 at 09:33 PM 0
Share

Not exactly. The "this" refers to the script itself. So if you have, say,

 var foo : int = 0;
 
 function bar (foo : int) {
     this.foo = foo;
 }

What you're saying is "Set the variable foo in this script to be the foo that gets passed in as an argument to the function. "this" happens to work with gameObject because gameObject is defined in $$anonymous$$onoBehaviour ( http://unity3d.com/support/documentation/ScriptReference/$$anonymous$$onoBehaviour.html ).

avatar image Ashkan_gc · Feb 26, 2010 at 04:47 AM 0
Share

@burnumed7 please use the @ notation when you want to answer another's question in my answer. i could not figure it out why you should tell this to me. @Random$$anonymous$$asure this keyword is a reference to current instance of the class that you are writing. at runtime this keyword is the class (script) that is executing. you can use this keyword and GetComponent to access anything you want in your gameObject.

avatar image
1

Answer by burnumd · Feb 02, 2010 at 09:10 PM

To access components between objects, use the GameObject.GetComponent family of methods (see also GetComponentInChildren, along with GetComponents and GetComponentsInChildren, which return an array of those components, if there are multiple instances of a component attached to an object). Eg:

var myComponent : ScriptType = targetGameObject.GetComponent (ScriptType); if (myComponent) { //do things with myComponent }

var myComponentArray : ScriptType[] = targetGameObject.GetComponents (ScriptType); for (var component : ScriptType in myComponentArray) { //Do something with an individual instance of that script. }

Where "ScriptType" is the name of the script (or other component, like ParticleEmitter) you're looking for and "targetGameObject" is the GameObject you expect to have the component (for example, the "other" in an OnTriggerEnter or what have you). Remember GetComponents always returns something while GetComponent will return null if the script isn't attached to the object you're checking.

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

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

Multiple objects with the same script having trouble accessing another script/object 1 Answer

How Can i Reference String another Script? 2 Answers

How to run function in another script with prefabs? C# 2 Answers

Best way to reference another same object 0 Answers

Better way of interacting and sharing variable between two scripts? 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