- Home /
trigger collision with three triggers help
Hello
I am making a top scrolling shooter following this tutorial http://walkerboystudio.com/html/unity_course_lab_2.html
function OnTriggerEnter (other : Collider)
{
if (other.gameObject.tag == "Bullet")
{
sceneManager.transform.GetComponent("script scene manager").AddScore(); //Adds a point for the astroid being hit
if (explosion) //Check for loaded explosion
{
Instantiate (explosion, transform.position, transform.rotation);//Create Explosion
audio.clip = playerDamage;
audio.Play();
}
ResetPosition(); //Resets the Astroid position
}
}
I have finished following the videos and am now doing the on your own section however I am running into a few problems. When I created the extra socket to fire another bullet, this instantiates a second bullet. However when two bullets collide with the asteroid at the same time it called the OnTriggerEnter function twice which will add two points, when only one should be awarded. Is there any way I can make it so that if two bullets collide at the same time the OnTriggerEnter is only called once, or if there is some alternate solution I haven't thought of.
Any help would be greatly appreciated :)
I don't think that there is really something like "at the same time". Even if it looks like that to you, chances are that there is a split second difference so you could try using a timer to check if the the OnTriggerEvent has been already called or perhaps a simple boolean variable would be enough?
Call onTriggerEvent > boolean = true
if(boolean==true){
//destroy bullet?
}
something like that might work
[edit] If there really is a chance of the bullets hitting the object at the exact same time you could try checking which object they interacted with and then add the score. In this case you could probably use a different (global?) script that (for example) contains a array with only unique entries. When you git a Astroid, you could add that to the list and do the rest as is.. and then go over that list and just add points for each astroid in the list as long as the list only contains unique entries.
I tried placing a print(Time.time); in to test, and it only prints once, so OnTriggerEnter is only being called once, but two points are still added
Are you certain that it's only calling it once? You might have "collapse" marked in your console. Check to see if there's a little number on the right end of the printout. If it's a 2, it's being called twice.
$$anonymous$$eep in $$anonymous$$d that Time.time doesn't change until the next frame, so no matter how long it takes for any one frame to completely process, it will not change the time until the next frame is loaded.
Also, it's a little off topic, but might I suggest not using "GetComponent" for a component that is constant? You should have an instance variable for that, and just have it saved before or just after the scene loads, so that it doesn't have to go searching for it every time (it's notably costly, computationally).
Your right collapse was checked, it is called twice
So I should just make the variable scene manager do get component when I declare it at the start of the script? This will avoid it calling it any time a collision happens?
You don't necessarily need to do GetComponent at all. If you have an instance variable in your script that is of the same type as the component you want to continuously reference, you can drag and drop it into that slot, in the editor, and it will remember to use that object in the reference, in play-mode.
For example:
var scene$$anonymous$$anager : ScriptScene$$anonymous$$anager;// Assu$$anonymous$$g that was the type name for that class
If you look at the component in the inspector, now, there should be a field labeled "Scene $$anonymous$$anager" that can hold a ScriptScene$$anonymous$$anager object. Simply drag and drop a GameObject with the ScriptScene$$anonymous$$anager on it into that slot (this even works if it is the same GameObject that this script is attached to).
Your answer
![](https://koobas.hobune.stream/wayback/20220613113432im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to add to a 3D object's length through code? 1 Answer
Overlap point of 2 kinematic colliders 1 Answer
How to add the right amount of points when my player destroys a gameobject? 0 Answers
Receiving information about getting hit by a Raycast 2 Answers
How do you execute Trigger-collider collision only in one gameobject? 1 Answer