Alright so, I have the problem that I have a Prefab of an Obstacle, and Player in the hierarchy. I need the game to restart when the Obstacle and Player collide with each other, but when I reference Player from the Hierarchy to the Prefab of an obstacle, it says "Type mismatch". Even though before it worked properly and both the Prefab and Player collided with each other
#Colliders not detecting
1 messages · Page 1 of 1 (latest)
Obstacles script: https://gdl.space/ebubohiqok.cs
Player's script: https://gdl.space/inuqawefoh.cpp
Here's the video of the problem
prefabs cannot reference in-scene objects. just use GetComponent on the incoming collider in OnCollisionEnter to get the PlayerMovement component
u can't assign a reference like that from within the scene
unless the prefab begins in the scene as well
you'll have to use scripting to assign it after u instantiate it into the scene..
using Awake(); or Start(); Usually Awake
technically they are using FindObjectOfType in Start, but they only ever use the reference to the player in OnCollisionEnter, so they should just get the reference to the player there
im supper confused about this now... before i could reference the player from the scene like that with no problems, but im not sure how did i do that exactly...
ill go learn about that then
and yes, that's exactly whay i have in my code
uuuhhh i dont know im super lost... how should i reference the player to the obstacle? ive used the GetComponent in OnCollisionEnter but it still doesn't work...
you need to be more specific than "doesn't work"
i mean, it's still the same. when i try to reference the player, it still tells me it's a Type mismatch
you don't need to be dragging the player in the inspector because you literally can't
i know
but im confused about it because it worked before
i mean
i could reference the player from the scene to the prefab
as i've already pointed out, prefabs cannot reference scene objects. it was not possible before so you were probably either dragging a prefab into the prefab or both objects were in the scene
no, i sweat i was dragging the player on to the prefab. im sorry for making such a mess but i have no idea how to crawl out of this dump...
*i swear
you can say that all you want, but i assure you it was not possible
i mean, i can't put the Obstacle prefab into the scene, maybe i can put the player into the prefabs folder and get rid of themfrom the hierarchy?
you are welcome to do that. but you do not need to drag the player into this script at all
i know that im missing something because before the Player could collide with the Obstacle, and the Obstacle would explode
you mean because it woulldn't work anyways?
well yes, but also you only use the reference in OnCollisionEnter so you only need to use GetComponent there. the reference to the player doesn't even need to be a class level variable. it can be a local variable only within that single if statement
but if your collision isn't working then we should be troubleshooting that
do you get errors in your console? if not, then the issue is likely the setup and not the actual code
if you are getting an error then please show the stack trace for the error as well as what the code currently looks like
NullReferenceException: Object reference not set to an instance of an object
PlayerMovement.Die () (at Assets/Scripts/PlayerMovement.cs:40)
ExplodedObstacle.OnCollisionEnter (UnityEngine.Collision collision) (at Assets/Scripts/ExplodedObstacle.cs:28)
UnityEngine.Physics:OnSceneContact(PhysicsScene, IntPtr, Int32)
and the code is still up here #1141096832081080460 message
looks like the issue is in the PlayerMovement class
you never assign an Animation to run
THAT MIGHT BE IT