#Colliders not detecting

1 messages · Page 1 of 1 (latest)

zinc pelican
#

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

nova phoenix
#

prefabs cannot reference in-scene objects. just use GetComponent on the incoming collider in OnCollisionEnter to get the PlayerMovement component

green tree
#

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

nova phoenix
#

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

zinc pelican
#

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...

zinc pelican
zinc pelican
zinc pelican
#

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...

nova phoenix
#

you need to be more specific than "doesn't work"

zinc pelican
#

i mean, it's still the same. when i try to reference the player, it still tells me it's a Type mismatch

nova phoenix
#

you don't need to be dragging the player in the inspector because you literally can't

zinc pelican
#

i know

#

but im confused about it because it worked before

#

i mean

#

i could reference the player from the scene to the prefab

nova phoenix
#

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

zinc pelican
#

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

nova phoenix
#

you can say that all you want, but i assure you it was not possible

zinc pelican
#

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?

nova phoenix
#

you are welcome to do that. but you do not need to drag the player into this script at all

zinc pelican
zinc pelican
nova phoenix
#

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

zinc pelican
#

well yeah, it does give me the error NullReferenceException

#

oh wait

#

naah im dumb

nova phoenix
#

if you are getting an error then please show the stack trace for the error as well as what the code currently looks like

zinc pelican
#

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)

nova phoenix
#

looks like the issue is in the PlayerMovement class

#

you never assign an Animation to run

zinc pelican