Method not working inside FixedUpdate
public RigidBody rb;
void FixedUpdate()
{
if (rb.position.y < -1)
{
FindObjectOfType<GameManager>().EndGame();
}
}
Error Message: 'Object.FindObjectOfType' is a method, not valid in the given context.
Tried in the regular void update()
, same results.
if Tried in the regular void update(), same results.why you say the problem is $$anonymous$$ethod not working inside FixedUpdate ??
I never used this method, but first i do is check Unity manual, and is not writed like this.. Please go read manual before come to ask.
https://docs.unity3d.com/ScriptReference/Object.FindObjectOfType.html
Answer by f03n1x · Feb 08, 2019 at 04:44 PM
I'm not sure how much of a performance hit finding objects of a specific type in a scene is but if you know for a fact that there is only ever going to be one GameManager within a scene you could setup the GameManager class like this:
public class GameManager : MonoBehaviour
{
public static GameManager instance;
Awake()
{
if(!instance)
instance = this;
}
}
That way you can call GameManager without having to do any searching at all, simply doing GameManager.instance.EndGame();
as for the issue of not being able to call the method from the FixedUpdate method, I'm not entirely what the issue is, I've used the same code on many occasions and was able to do things like get components and call methods like it was a regular GetComponent call.
Answer by UltraGearGames · Feb 08, 2019 at 12:25 PM
I think you should do
GameObject gameManager = GameObject.FindObjectOfType<GameManager>().EndGame();
in Update() or FixedUpdate() of course.
What did i get a down vote for?! Im just desperately trying to get 15 rep to be able to ask questions! Do i really want that much? Be able to use Unity Answers? I dont know what to do anymore......