- Home /
have problem in OntriggerEnter (with different objects) with GUItexture
Hello, Im new here, with Unity 3D as well, Im doing some script to let the user interact with 8 planets of the solar system. All I want is: when the user enter 1 planet, the GUItexture (which is show the Photo of that planet) is on and the sound of plannet play. When the person enter the 2nd time, the Guitexture is Off.
Im trying to code and Add into the Guitext (which is called PlanetCollect):
static var planet : int = 0; var Jupiter : Texture2D; var Earth : Texture2D; var Neptune : Texture2D; var Saturn : Texture2D; var Mercury : Texture2D; var Venus : Texture2D; var Mars : Texture2D; var Uranus : Texture2D;
function Start(){ guiTexture.enabled = false; planet = 0; }
function Update () { if(planet == 1){ guiTexture.texture = Jupiter; guiTexture.enabled = true; } else if (planet == 2){ guiTexture.texture = Earth; guiTexture.enabled = true; } else if (planet == 3){ guiTexture.texture = Neptune; guiTexture.enabled = true; } else if (planet == 4){ guiTexture.texture = Saturn; guiTexture.enabled = true; } else if (planet == 5){ guiTexture.texture = Mercury; guiTexture.enabled = true; } else if (planet == 6){ guiTexture.texture = Venus; guiTexture.enabled = true; } else if (planet == 7){ guiTexture.texture = Mars; guiTexture.enabled = true; }
else if (planet == 8){
guiTexture.texture = Uranus;
guiTexture.enabled = true;
}
}
Then I creat another one called PlayerCollisions.Js. And add into First Person Controller:
var JupiterCollect : AudioClip; var EarthCollect : AudioClip; var NeptuneCollect: AudioClip; var SaturnCollect : AudioClip; var MercuryCollect : AudioClip; var VenusCollect : AudioClip; var MarsCollect : AudioClip; var UranusCollect : AudioClip;
function OnTriggerEnter(collisionInfo : Collider){ if(collisionInfo.gameObject.tag == "jupiter"){ JupiterCollect.planet=1; audio.PlayOneShot(JupiterCollect);
} else if(collisionInfo.gameObject.tag == "earth"){ EarthCollect.planet=2; audio.PlayOneShot(EarthCollect); } else if(collisionInfo.gameObject.tag == "neptune"){ NeptuneCollect.planet=3; audio.PlayOneShot(NeptuneCollect); } else if(collisionInfo.gameObject.tag == "saturn"){ SaturnCollect.planet=4; audio.PlayOneShot(SaturnCollect); } else if(collisionInfo.gameObject.tag == "mercury"){ MercuryCollect.planet=5; audio.PlayOneShot(MercuryCollect); } else if(collisionInfo.gameObject.tag == "venus"){ VenusCollect.planet=6; audio.PlayOneShot(VenusCollect); } else if(collisionInfo.gameObject.tag == "mars"){ MarsCollect.planet=7; audio.PlayOneShot(MarsCollect); } else if(collisionInfo.gameObject.tag == "uranus"){ UranusCollect.planet=8; audio.PlayOneShot(UranusCollect); }
else planet=0; }
And the 8 var jupiter, earth... have errors is: unknown Identifier and 'planet' is not a member of 'UnityEngine.AudioClip'
Answer by Jesus_Freak · Nov 18, 2010 at 08:22 PM
nevermind, i found out what was wrong. in the second code, it says something.planet = 1; right? well, the var to explain that identifier is in your other script, so you have to make it a static var for another script to recognize it.
Answer by Design3.com · Nov 25, 2010 at 01:54 AM
Your first script is named PlanetCollect, right? I think you want the conditional statements in the PlayerCollision script to read:
if(collisionInfo.gameObject.tag == "jupiter"){
PlanetCollect.planet=1;
audio.PlayOneShot(JupiterCollect);
} else if(collisionInfo.gameObject.tag == "earth"){
PlanetCollect.planet=2;
audio.PlayOneShot(EarthCollect);
}
The static variable ("planet") belongs to the script that declared it ("PlanetCollect.js"). To access the variable in other scripts you have to refer to it as ParentScript.staticVariable (in this case, "PlanetCollect.planet").
You could further simplify this by using a builtin array to store your planet textures and audio clips:
var planetTexture : Texture2D[];
function Update () { for (i=0;i<planetTexture.length;i++) { guiTexture.texture = planetTexture[i]; guiTexture.enabled = true; } }
then in PlayerCollision:
var planetAudio : AudioClip[];
function OnTriggerEnter(collisionInfo : Collider){ for (i=0;i<planetAudio.length;i++) { if(collisionInfo.gameObject.tag == i.ToString()){ PlanetCollect.planet=i; audio.PlayOneShot(planetAudio[i]); } } }
To make this work you'd have to tag the planets as "0", "1", "2", etc.
Our site, design3, has a bunch of tutorials on scripting and Unity in general if you want more info on the subject.
Your answer
Follow this Question
Related Questions
Raycast Destroy(hit.collider.gameObject); (Still need help) 1 Answer
Appear and Dissapear: Fog 1 Answer
Several colliders to one script 1 Answer
How to change pixel inset of GUITexture? 2 Answers
how to resize box collider? 4 Answers