• 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
2
Question by MrBunny · May 02, 2014 at 07:03 PM · gameobject

How do I have Reference to GameObject

I'm just learning unity and made a quick script that on click of the item it removes the gameObject. I see that my script inherits MonoBehaviour which not to sure what that is. But my question is how do I have reference to things like gameObject and Destory? How is it that the unity engine is able to provide those items within the scope of my script?

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
22

Answer by theredace · May 02, 2014 at 07:58 PM

MonoBehaviour is the native Unity class that all scripts naturally inherit from. You can read about "inheritance" in coding, it just means that this current script, which is a class, inherits all the information from another class as if all the code in that other class existed within the current class/script. All the typical Unity methods, functions, etc. exist within the MonoBehaviour class, so if you're going to use any of that stuff then your script needs to inherit from MonoBehaviour:

http://docs.unity3d.com/Documentation/ScriptReference/MonoBehaviour.html

Luckily, Unity does this automatically which is why you see "class : MonoBehaviour{" at the top of your scripts. If your script is attached to a gameobject, it MUST inherit from MonoBehaviour. You can have scripts that don't inherit from MonoBehaviour, but they can't be attached to a gameobject. This would be like if you created a custom class that you instantiate into other scripts, but don't get bogged down with that concept yet if you're just getting started.

"Referencing" is what you have to do if you ever want your script to have control of something other than the gameObject it's attached to or that gameObject's Transform component. If you want to do something with some other component on that same gameObject (like a collider, a rigidbody, a light, etc), or if you want to do something with a completely different gameObject and/or it's components, you must create a reference to that gameObject or component. Creating a reference is just a fancy way of saying that you create a variable to hold the reference to that gameObject or component, and then you use some method to tell the variable what it should be a reference to.

Your variable will be of type "GameObject" if it's supposed to reference a gameObject, or the type will be a component name if it's referencing a component (including scripts... the variable type will be the script name). Examples:

 GameObject someGameObject;
 GameObject someOtherGameObject;
 Rigidbody someRigidbody;
 Light someLight;
 ScriptName someScript;

Now that you have the variables you need, you must tell them what they are. If you make the variables public, like public GameObject someGameObject then it'll give you a field in the inspector to drag the gameObject to and that will create the reference (you can do the same with component variables... just drag any gameObject that has that component type and it'll reference the component on that gameObject). Note that you can't do this if your current gameObject (the one you're script is on) is a prefab.

If you can't make it a public variable, then you can assign it a reference in code using some other method. Like just using the gameObject's name:

 someGameObject = GameObject.Find ("GameObjectName");

Or with a "tag", if the object has that tag applied:

 someGameObject = GameObject.FindWithTag ("SomeTagName");

If it's a component you need to reference, then you need to use the GetComponent() function. Like if the component you're referencing is on the same gameObject:

 someRigidbody = GetComponent<Rigidbody>();

Or if the component is on a different gameObject then you can combine these concepts:

 someScript = GameObject.Find ("GameObjectName").GetComponent<ScriptName>();

It seems kinda complicated at first, but once it clicks it's really simple to use. Just remember the basic rules that if you want to access a different gameobject, or any component other than the Transform component (because it's static so you don't have to use GetComponent), you need to create a reference to it. Make a variable to hold your reference, then use the simplest method to assign something to that reference.

Hope that helps!

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 theredace · May 02, 2014 at 08:12 PM 0
Share

Also, since you asked about Destroy(), that is a built-in function in the GameObject class, and in each unique componenet class:

http://docs.unity3d.com/Documentation/ScriptReference/Object.Destroy.html

You can destroy a GameObject or a component. So, using the ideas from above, once you have a reference to a gameObject you want to destroy, you just put that reference variable name in the parentheses:

 Destroy(someGameObject);

Voila! You destroyed whatever gameObject that variable is referencing. You don't even need to have the reference to the GameObject yet, you can get the reference inside the Destroy() function if you want:

 Destroy(GameObject.FindWithTag ("SomeTagName"));

And the same idea with components:

 Destroy(someRigidbody);

One more thing you should probably know... the word "this" refers to the script you're writing. So if you see something like this.gameObject, it means "this script's gameObject". Though it's generally not necessary to say this, because simply saying "gameObject" automatically refers to whichever gameObject this script is attached to.

avatar image
1

Answer by Oribow · May 02, 2014 at 07:05 PM

They are function of MonoBehaivior look here

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.

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

25 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

using Contains(gameObject) to find and destroy a gameObject from a list 2 Answers

When fliping the character the rotation of character's weapon goes backward 0 Answers

How do I assign a prefab reference to a private variable? 1 Answer

Cannot convert 'UnityEngine.Collider[]' to 'UnityEngine.gameObject[]' using OverlapSphere 3 Answers

GameObject not working with variable in C# script 3 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