https://sesayrose12.wixsite.com/opportunitygamedev/post/c-not-calling-the-restart-why-is-that
my C# is not calling Restart. where did i go wrong... UNITY
You have probably worked it out by now, but I dare say it is because of a little typo… onCollisionEnter() should be OnCollisionEnter()
Also, FindObjectOfType() is very heavy and may not be ideal for your situation. IMO; your GameManager.cs would be an ideal candidate for a Singleton - love me a Singleton! - or if you don't want to go down that rout maybe ad a private variable to the class(s) you want to call it from, and set the value in Awake() or Start().
@Eli_SBG Hey thanks for the response, I've figured where i went wrong. I just had to assign some tags and scripts to some components that i had added. Silly mistakes. You're right about OnCollision but FindObjectOfType() worked
opport_UNITY said:
@Eli_SBG Hey thanks for the response, I've figured where i went wrong. I just had to assign some tags and scripts to some components that i had added. Silly mistakes. You're right about OnCollision but FindObjectOfType() worked
FindObjectOfType might work, but it is imperformant and should thereby not be called more often than needed. Unity isn't really that great performance-wise anyways, so you might want to at least try to avoid obvious efficiency pitfalls. It shouldn't matter here as the Find + EndGame-call probably only happen once though, but in general, if an object is accessed multiple times there are some better solutions.
I wouldn't advice for singletons though, they cause nothing but troubles (especially in Unity where every object is bound to a scene). You should try to just cache the FindObjectOfTypeCall once, if you :
public class PlayerCollision : MonoBehaviour
{
private GameManager gameManager;
private void Awake()
{
gameManager = FindObjectOfType<GameManager>();
}
void OnCollisionEnter(Collision collision)
{
gameManager.EndGame();
}
};