Hey all, I am more than new to the world of JavaScripting and Unity and am in dire need of some assistance. I am trying to make it so that my character can collect these little cheese cubes hidden throughout the game. I want them to disappear after being picked up, play a sound and add to a on-screen counter. After searching the internets, I parsed this together:
private var Collectible_Cheese : int= 0; //Set up a variable to store how many you’ve collected
public var collectedSound : AudioClip;
var Cheese_Cube : ObjectNames;
function OnGUI(){
GUI.Label(new Rect(10, 10, 50, 20), “Collected:” + Collectible_Cheese);
}
function OnTriggerEnter(other : Collider){ //Checks to see if you’ve collided with another object
if(CompareTag(“collectable”)){ //checks to see if this object is tagged with “collectable”
audio.PlayOneShot(collectedSound); //plays the sound assigned to collectedSound
Collectible_Cheese++; //adds a count of +1 to the collected variable
Destroy(Cheese_Cube.gameObject); //destroy’s the collectable
}
}
function Start () {
}
function Update () {
}
Much to my surprise, I am able to play the game before any compiler errors occur. This one says “UnityException : Tag: collectable is not defined!” … but I have added it under the Tag Manager and assigned it to the cheese cube object itself.
Have you checked if you have accidentally left a letter in a capital letter? Tags are case-sensitive.
EDIT
Ignore what I said earlier about the check. It looks like you are comparing the player’s tag, not the collided object (which is your cheese object). You’ll want to do this instead:
function OnTriggerEnter(other : collider)
{
if(other.CompareTag("collectable"))
{
// Do Something
}
}
In that case, have you check for case-sensitive? Otherwise, you can try:
function OnTriggerEnter(other : collider)
{
if(other.gameObject.tag.ToLower() == "collectable") // Force the tag to lower to ignore case-sensitive
{
// Do Something
}
}
That has gotten rid of the error message, but the cheese cube doesn’t disappear or make any sort of counter appear. I thought that’s what the GUI part of the code would do, but obviously not yet haha.
Now “Collecte” appears in the top left hand corner of the screen from the moment I hit play…it doesn’t change when I interact with the cube and the cube still stays there.
private var Collectible_Cheese : int= 0; //Set up a variable to store how many you've collected
public var collectedSound : AudioClip;
var Cheese_Cube : ObjectNames;
function OnGUI(){
GUI.Label(new Rect(10, 10, 50, 20), "Collected:" + Collectible_Cheese);
}
function OnTriggerEnter(other : Collider){ //Checks to see if you've collided with another object
if(CompareTag("collectable")){ //checks to see if this object is tagged with "collectable"
audio.PlayOneShot(collectedSound); //plays the sound assigned to collectedSound
Collectible_Cheese++; //adds a count of +1 to the collected variable
Cheese_Cube = other.gameObject; //<------ This is the line you've forgotten :smile:
Destroy(Cheese_Cube);} //<------ and edited this... :3
}
Thanks aixaix, I put that code into my script like this:
private var Collectible_Cheese : int= 0; //Set up a variable to store how many you've collected
public var collectedSound : AudioClip;
var Cheese_Cube : ObjectNames;
function OnGUI(){
GUI.Label(new Rect(10, 10, 100, 20), "Collected:" + Collectible_Cheese);
}
function OnTriggerEnter(other : Collider){ //Checks to see if you've collided with another object
if(CompareTag("collectable")){ //checks to see if this object is tagged with "collectable"
audio.PlayOneShot(collectedSound); //plays the sound assigned to collectedSound
Collectible_Cheese++; //adds a count of +1 to the collected variable
Cheese_Cube = other.gameObject;
Destroy(Cheese_Cube); //destroy's the collectable
Debug.Log("message");
}
function Start () {
}
function Update () {
}
And got a bunch of compiler errors about my “;” use and my “(” use…I didn’t see any syntax issues, do you guys?? Thanks again for the help!
Wow that was stupid. Everything is running smoothly now, except that it will not destroy the Cheese_Cube. I made some changes to the code to try and get rid of some in-play compiler issues, which are now resolved, and it now looks like this:
private var Collectible_Cheese : int= 0; //Set up a variable to store how many you've collected
public var collectedSound : AudioClip;
var Cheese_Cube : GameObject;
function OnGUI(){
GUI.Label(new Rect(10, 10, 100, 20), "Collected:" + Collectible_Cheese);
}
function OnTriggerEnter(other : Collider){ //Checks to see if you've collided with another object
if(CompareTag("Collectable")){ //checks to see if this object is tagged with "collectable"
audio.PlayOneShot(collectedSound); //plays the sound assigned to collectedSound
Collectible_Cheese++; //adds a count of +1 to the collected variable
Cheese_Cube = other.GameObject;
Destroy(other.GameObject); //destroy's the collectable
Debug.Log("message");
}
}
function Start () {
}
function Update () {
}
So close! Even the counter is counting it right! Which is awesome! Thanks for all the help!
So I got the Cheese to disappear, but the minute I make the cheese_cube inactive my counter disappears. This is what my codes is right now:
private var Collectible_Cheese : int= 0; //Set up a variable to store how many you've collected
public var collectedSound : AudioClip;
var Cheese_Cube : GameObject;
private var Collected : int;
Cheese_Cube.active = true; //Using True/False techno
function OnTriggerEnter (other : Collider) {
audio.PlayOneShot(collectedSound); //plays the sound assigned to collectedSound
Collectible_Cheese++; //adds a count of +1 to the collected variable
Cheese_Cube.active = false; //destroy's the collectable
GUI.Label (new Rect (10, 10, 100, 20), "Collected:" + Collectible_Cheese);
if (Cheese_Cube.active = false){
GUI.Label (new Rect (10, 10, 100, 20), "Collected:" + Collectible_Cheese);
Collectible_Cheese++;
}
}