Hello All,
I am having a small problem with my script I’m sure everything is linked up correctly but cannot figure what it is I am doing wrong - probably something stupid:sweat_smile:. The problem is twofold firstly I cannot get the sound to play when the Trigger is activated and I cannot get the gameoject to recognise what it is that hit it namely the player.
The script is this:
void OnTriggerEnter(Collider Other){
print("Collision");
if (Other.gameObject.tag == "Player"){
//print to console not working
print("collision with player Car");
//playsound here
//not working?!?
}
//sound not working here either
audioSource.PlayOneShot(checkPoint, 1f);
//both these work
halfwayTrigger.SetActive(false);
finishTrigger.SetActive(true);
}//end OnTriggerEnter()
To anyone who can help here is a big thankyou in advance.
Regards.
Just tidied it up a bit, still broken however. We need more information.
How are you assigning ‘checkPoint’. How are you assigning ‘audioSource’. In the inspector? Are they actually assigned?
// Lowercase o for 'other' - Naming convention for variable names
void OnTriggerEnter(Collider other)
{
// I assume this first print actually displays in the console
print("Collision");
// The preferred way to check tags is now CompareTag
if (Other.gameObject.CompareTag("Player") == true) {
// print to console not working
print("collision with player Car");
}
// How are you assigning this 'audioSource'?
// Do you get any errors?
// Do you actually have this sound 'checkPoint'?
audioSource.PlayOneShot(checkPoint, 1f);
halfwayTrigger.SetActive(false);
finishTrigger.SetActive(true);
}
Thanks for your reply!
Thankyou for explaining the newer way of using tags didn’t know anything about that.
With the code Line 5, where I have print(“Collision”), it actually does work.
Line 7, still doesn’t respond to the Car object ie “Player”. As far as I know the Car is correctly tagged as “Player”, as it is listed in the Inspector as such. Could it be because the Car Object has defined it’s own colliders?
I had a lot of trouble adding sound in this 3D project so I have been using the method of publicly assigning an AudioClip and then using an AudioSource to play it. I couldn’t figure out any way to do it as using just an AudioSource and using whatever.Play(); wasn’t working?!? And did it this way as I found it in the Unity Docs. So, AudioSource is assigned in the Start() method:
//AudioClip defined at top of class
//linked in thru Inspector
public AudioClip checkPoint;
void Start () {
audioSource = GetComponent<AudioSource>();
}
So, any advice to get the sound working would be greatly appreciated