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 MrMcKinle · Jun 26, 2013 at 08:33 PM · c#gameobjectaudioitem

this.GameObject - Uses?

I've been looking through the documentation trying to find this "phrase" (if that term makes sense) of code and the explanation of its uses. I'm trying to script my AudioSource to enabled/disable on the game object that it's attached to, but cannot seem to get it to work.

I'm using C# and can't even manage to enable/disable the AudioSource with the 'GameObject.Find' phrase. I've looked through the documentation concerning that too but have also had no luck. However, I may have missed it because the docs are very hard to navigate and the sections aren't very clear on what they really contain. So forgive me if I've overlooked anything.

I'm trying to get a sound to play as an item is being collected. I have multiple collectable items and wish not to create new scripts for every item. Therefore, I'm trying to implement "this.gameObject" in my script, but for some reason it's causing problems. If anyone can provide assistance, it would be greatly appreciated.

This is my entire script. "Destroy (this.gameObject);" works, but how should it be used for what I need?

using UnityEngine; using System.Collections;

public class CollectItem : MonoBehaviour { public GameObject page; private bool showItemMessage = false; //private bool showCollectedMessage = false; private bool canbecollected = false; //private int guiTime = 3;

 // Use this for initialization
 void Start () 
 {
     page = GameObject.Find ("PageTest");
 }
 
 void Update()
 {
     collectItem ();
 }
 
 // Update is called once per frame
 void OnTriggerEnter () 
 {
     showItemMessage = true;
     canbecollected = true;
 }
 
 void collectItem ()
 {
     if(canbecollected)
     {
         if(Input.GetKeyUp (KeyCode.C))
         {
             page.GetComponent(AudioSource).enabled = true;
             Destroy (this.gameObject);
             //showCollectedMessage = true;
             //showItemMessage = false;
             //StartCoroutine(GuiDisplayTimer ());
         }
     }
 }
 
 void OnTriggerExit ()
 {
     showItemMessage = false;
     canbecollected = false;
 }
 
 void OnGUI ()
 {
     if(showItemMessage)
     {
         GUI.Box(new Rect(Screen.width/4, Screen.height/4, 300, 25), "Press C to collect page.");
     }
     
     //if(showCollectedMessage)
     //{
     //    GUI.Box(new Rect(Screen.width/2, 50, 200, 25), "Item Collected");
     //}
 }
 
 //IEnumerator GuiDisplayTimer()
 //{
 //    yield return new WaitForSeconds(guiTime);
 //    
 //    showCollectedMessage = false;
 //}

}

Comment
Add comment · Show 1
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 Jessy · Jun 26, 2013 at 08:45 PM 2
Share

"this" is useless/redundant here. All of your non-static variables can be accessed with "this" or without, so don't bother with the extra code when you don't need to differentiate from some other variable with the same name.

1 Reply

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

Answer by Em3rgency · Jun 26, 2013 at 09:29 PM

You never have to use this.gameObject. 'this' is redundant. When talking about the object that the script is attached to, you never have to specify. You can just use the methods out of thin air.

Anyway, to access a script that is attached to the same object, and disable it, you can use:

 GetComponent<otherScriptName>().enabled = false;

Note, the HAVE TO STAY.

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 MrMcKinle · Jun 26, 2013 at 09:30 PM 0
Share

Thank you. This worked and I found a tutorial shortly after. $$anonymous$$y collection script is now working perfectly.

avatar image Em3rgency · Jun 26, 2013 at 09:33 PM 0
Share

Glad to have helped, happy coding!

avatar image BlueRaja_2014 · Jun 26, 2013 at 09:37 PM 0
Share

technically otherScriptName is the name of the class, not the name of the script.

avatar image Em3rgency · Jun 26, 2013 at 09:44 PM 0
Share

A technicality that would often just confuse people new to coding. I said script name on purpose.

avatar image MagicStyle · Oct 24, 2017 at 03:19 PM 0
Share

I just tried "SetActive(false)" to deactivate the object where the script is attached to but it does not work.

avatar image Bonfire-Boy MagicStyle · Oct 25, 2017 at 09:03 AM 0
Share

@$$anonymous$$agicStyle. You need to put

  gameObject.SetActive(false)

or

  this.gameObject.SetActive(false)
 

It's the this that's not needed, not the gameObject.

The answer is incorrect (or, at least, misleading) when it says that "You can just use the methods out of thin air". SetActive is a function of a GameObject, so you do need to get the GameObject in order to call it.

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

20 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

Related Questions

Make sound play at same time as object moves 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Volume Control with Slider 1 Answer

referencing a gameObject with it's own script 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