Instantiating using new keyword

Hi,
I am trying to instantiate ScoreKeeper scoreKeeper = new ScoreKeeper();
But this isn’t allowed in Unity as you cannot instantiate anything inheriting from MonoBehaviour class.I am still not clear why then we use ScoreKeeper scoreKeeper = FindObjectOfType();
what purpose this statement achieves.
Although I know its searching for an object of type ScoreKeeper.

Hi there, you should be able to simply say ScoreKeeper scoreKeeper, no need to use new which is of creating structs like Vector 3.

@sampattuzzi may be able to clarify further regarding this course’s specifics.

ScoreKeeper is created by the scene by having the component on a GameObject. FindObjectOfType will find the object that was already created.

Thanks for your answer,but am still confused with the purpose of using ScoreKeeper scorekeeper=FindObjectOfType();
Is this the only way we can instantiate objects of a class inheriting from a MonoBehaviour type?

No, we can also attach components programmatically.But they need to be on a game object. gameObject.AddComponent<ScoreKeeper>()

I see thanks very much

Privacy & Terms