- Home /
Checking collision between 2 objects on 3rd object
Hi,
on my game I have a prefab object called "thebullet". This bullet has a script that when it collides with tag "wall" and "enemy" it gets destroyed.
I have a player object with 2 scripts one for movement and the other shoots the bullet game object. I would like on this script to check to see if the bullet and enemy have collided and if they have increase a score count.
I cannot add the code to the enemy because each enemy will have its own count. I cannot seem to add this script to the bullet because its a prefab object and isn't listed on the hierarchy. So I am left to add it to my "playerShooting" code.
void OnCollisionEnter(Collision collision)
{
if(collision.gameObject.tag == "Enemy")
{
count = count + 1;
SetScore();
}
This code will not work as its checking to see if the player and the enemy touch. I need to see if GameObject thebullet and the objects tagged as "enemy" have touched. All I know is there is the " FindObjectOfType(typeof(GameObject));" but I have had no luck using that.
You can add scripts to prefabs fine in the project window, what's the problem?
Yes. $$anonymous$$y prefab has a script that checks if it contacts walls and enemys. But when I add a public GUIText I can see it listen on the inspector but I cannot link my "GUItextScore" object to the bullet prefab.
I can see it listened but when I drag and drop it into place it wont work. underneath I can see my public int count and I can set that a value within unity.
$$anonymous$$y university lecture said it might be a problem with the fact that I'm trying to do this with a prefab but he only had a quick look.
Isn't the score count something in the world or level class. If you make that public or create a getter and setter for it you can change the score. (if you give the bullet script access to that)
thef1chesser that is a good idea. I didn't know I could write script for the level but that does make a lot of sense. I have only been doing unity for 4 weeks at university. This sounds like a good idea if the objects can reference each other. I do not know anything about "getter and setter" but I will look it up.
Thanks for the suggestion!
Your answer
Follow this Question
Related Questions
My weapon takes damage. help... 1 Answer
Distribute terrain in zones 3 Answers
animation.isPlaying always true and Animation stuck at time 0 0 Answers
Problem Detecting 2D Collisions 1 Answer
How to move an object over a distance in a direction? 2 Answers