- Home /
The question is answered, right answer was accepted
Spawn and Destroy problem snake like
Hello everyone...so, i am making a game that has a "snake-ish" mechanic for the food spawn, let's say... When my game starts, it spawns the food prefab at a random location, and that's good. What i can't achieve is that when my player touches the food, that food piece disappiears and a new one is spawned again at a random location...i tried EVERYTHING. But nothing seems to work and it freaks me out :/ it just passes trhough and nothing happens...i really hope some of you can help me with this one!
i tried On collision enter, i tried calling the same spawn function from another script, i tried to make a new spawn function aswell...nada
Can you please share some more information on your setup? Whats inside of your Ontriggerenter? Does it not get called or just doesnt work? did you set up colliders correctly? do you have rigidbodys attached?
please provide more information. There will probably noone be able to help you like this.
Answer by tormentoarmagedoom · Sep 30, 2019 at 03:49 PM
Hello there.
You are almos in the correct solution. First, do you know what is not working? Is OnCollisionEnter beeing called?, Where is this script attached? If you attach it in the food, dont need to "find for the foods in the scene. Are you sure your food is tagged as food? etc...
You have to use OnCollisionEnter, but, first, take care there are 2 methods: OnCollisionEnter and OnCollisionEnter2D. One for each type of colliders 2D or 3D
Then, you need to be sure the cmethos is beeing called when the collision ocurs, a good way to know it is by using a Debug.Log command
Then, make sure everything is correct with the method (have colliders, not mark as trigger, with rigidbodies...
When you are sure the method is called, then staert thinking i how to make a new food, but while programing, if you are new, NEVER TRY more than 1 thing at once. Go step by step, and dont continue until you are sure one thing is working!
Good luck!
Thanks for the answer, i fixed it myself but still ;) Have a nice day and good luck on your projects too!
Answer by ITSLEO · Sep 30, 2019 at 03:52 PM
Guys. I fixed it myself. So i'm posting with the hope that this might help you aswell. So let's start by saying that sometimes C# it's dumb and makes your code a useless piece of trash and desperation even if there are no errors and in theory it should work. Now...
If sometimes you need to check a collision to trigger something, and OnCollisionEnter doesn't work: use OntriggerEnter instead change "other" to "collider" use the gameobject tag to do stuff, not the rigidbody, but still, one of the two objects must have the rigidbody, and i suggest you to use the object that is more still as the one with the rigidbody :)
First, C# it's not dumb, you just dont understand whats going on.
and in theory it should work..? Unity can not be aware of logic errors.
OntriggerEnter is for trigger coliders OnCollisionEnter is for non triger coliders
"hange "other" to "collider" use the gameobject tag to do stuff, not the rigidbody" .... Read the Unity Documentation before please.....
So please, dont do this... Some new people come here looking for knowledge and wisdom, cant come and start giving advices like this..
Follow this Question
Related Questions
SteamVR - Spawning Prefab on Controller,SteamVR - Spawning Objects on Controller 0 Answers
Spawn an object with additional parameter 1 Answer
Function call is not working 0 Answers
How to alter c# script from a script in a different scene (i.e. display score in different scene) 2 Answers
Checking if a function has been called from another script 2 Answers