- Home /
Needing help with Triggers
Alright i am new to Unity and Never Scripted before. and so far I am the only one in my Company for games (Celestial incorporation) and until i find someone that will work for free ( until we get out there and at least somewhat popular) then I will be working alone. and i have a pretty decent amount done i watched a large variety of tutorials. on youtube. and i can't figure out how to make a trigger event script only effect one thing for instance i have Voice overs in game, and a coin collection system, if i enable both of them then well... only the coin collection happens. i will show both of my Scrips here. Can anyone help me out a bit? i have been working on it for few days now. all my scripts are simple for the most part.
this is the Coin Collection System
#pragma strict
function Start () {
}
function Update () {
}
function OnTriggerEnter(col : Collider)
{
PlayerInventory.Coin += 10;
print(PlayerInventory.Coin);
Destroy(col.gameObject);
}
this is the Voice over on trigger script
function OnTriggerEnter (other : Collider)
{
audio.Play();
}
hopefully it is a easy fix and i strike some luck, i need it, I appreciate all and any help i get.
Alright i highlighted the scripts and clicked that 101010 button hope it helps.
Reformat it how ? all i did was copy and paste it from $$anonymous$$onoDevelop so what format should i edit it in ?
Use the small button with 1's and 0's to make the code readable.
Answer by GameVortex · Nov 14, 2013 at 08:26 AM
Hi
It would seem you are destroying the gameobject that triggers the coin collection system. So the gameobject will not trigger the Voice over because it no longer exists.
If the audio should always play when the coin collection system is triggered then you should probably trigger the audio in the coin collection system instead, before the gameobject is destroyed.
If you need to have them separate (which I would not recommend, you cannot guarantee that the gameobject will hit both colliders), then you will have to check if the gameobject has hit both trigger before destroying it.
So when i combine the scripts i should just put the Voice over script ( second one ) in the function Update ? sorry as i said i am slow at this kinda stuff. but im dedicated to it. but would that work or would i have to make a new section for that audio under the collection system? like this.
pragma strict function Start () {
}
function Update () {
}
function OnTriggerEnter(col : Collider) { PlayerInventory.Coin += 10; print(PlayerInventory.Coin); Destroy(col.gameObject); }
function OnTriggerEnter (other : Collider)
{
audio.Play();
}
sorry if these are like really hard to understand questions this is the first time i asked for help over the web ever >,..,> so i have no clue how to ask things correctly in forums and what not.
No worries, everybody starts somewhere. First thing is that when you want to insert code into your comment or question you should use the button with 1's and 0's, the one next to the attachment button. That will format it properly. And I would recommend checking this video out: Unity Answers Tutorial It is an introduction to using unity Answers.
Now back to the question. You cannot have two OnTriggerEnter functions in one script. So what I meant was that you should put your audio.Play() right before you call the Destroy function like so:
function OnTriggerEnter(col : Collider)
{
PlayerInventory.Coin += 10;
print(PlayerInventory.Coin);
audio.Play();
Destroy(col.gameObject);
}
This will ensure that both things will happen before the object is destroyed.
Alright ill keep that in $$anonymous$$d, i whent a head and edited the Post. With it being put like that, how you put it, would it allow the collection system to effect along with the separate trigger for voices, or eveytime i collect something a voice turns on ?
By doing it like this the voice will play every time you collect something. If that is not what you want you will have to add a check for if the audio is supposed to play or not.
Your answer