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 Celestial_Incorporation · Nov 14, 2013 at 05:30 AM · triggers

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.

Comment
Add comment · Show 4
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 mattssonon · Nov 14, 2013 at 08:26 AM 0
Share

Please reformat your code so it's readable.

avatar image Celestial_Incorporation · Nov 14, 2013 at 11:27 PM 0
Share

Reformat it how ? all i did was copy and paste it from $$anonymous$$onoDevelop so what format should i edit it in ?

avatar image mattssonon · Nov 15, 2013 at 08:17 AM 0
Share

Use the small button with 1's and 0's to make the code readable.

avatar image Celestial_Incorporation · Nov 16, 2013 at 02:26 PM 0
Share

I edited it so it is in correct format.

1 Reply

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 4 · 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 Celestial_Incorporation · Nov 14, 2013 at 11:34 PM 0
Share

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.

avatar image GameVortex · Nov 15, 2013 at 09:11 AM 0
Share

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.

avatar image Celestial_Incorporation · Nov 16, 2013 at 02:25 PM 0
Share

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 ?

avatar image GameVortex · Nov 18, 2013 at 01:57 PM 0
Share

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

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

17 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Objects act like buttons 1 Answer

How many triggers is too many? Do they affect performance too much? 1 Answer

Display GUI Text on Trigger Enter 1 Answer

How to import the object from server to unity 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