- Home /
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;
//}
}
"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.
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.
Thank you. This worked and I found a tutorial shortly after. $$anonymous$$y collection script is now working perfectly.
technically otherScriptName
is the name of the class, not the name of the script.
A technicality that would often just confuse people new to coding. I said script name on purpose.
I just tried "SetActive(false)" to deactivate the object where the script is attached to but it does not work.
@$$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
Follow this Question
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