so have script counts points when collide tagged game objects. want game make different sound when hit different objects. here script:
using unityengine; using unityengine.ui; using system.collections; public class points1 : monobehaviour { public text counttext; public text wintext; private int count; void start() { count = 0; setcounttext(); wintext.text = ""; playerprefs.setint("score", count); playerprefs.save(); count = playerprefs.getint("score", 0); } void ontriggerenter(collider other) { if (other.gameobject.comparetag("pickup")) { other.gameobject.setactive(false); count = count + 100; setcounttext(); } else if (other.gameobject.comparetag("minus300")) { other.gameobject.setactive(false); count = count - 300; setcounttext(); { getcomponent<audiosource>().play(); } } playerprefs.setint("score", count); playerprefs.save(); count = playerprefs.getint("score", 0); } void setcounttext() { playerprefs.setint("score", count); playerprefs.save(); count = playerprefs.getint("score", 0); counttext.text = "score: " + count.tostring(); if (count >= 5000) { wintext.text = "good job!"; } } }
so how have different sound pickup object , minus300 object? thank you!
you can link audio sources in fields, , set them in inspector in unity editor:
public class points1 : monobehaviour { public audiosource pickupaudio; public audiosource minus300audio; // ... use pickupaudio , minus300audio instead of getcomponent<audiosource>()
an alternative more complex situations use getcomponents<audiosource>()
array of audiosource
components, iterate through them find right one. not less clear current situation, it's slower--although may necessary in cases.
Comments
Post a Comment