- Home /
Question by
IamJarek · Aug 06, 2017 at 08:05 PM ·
c#scripting problemupdatemouselook
Object reference not set to an instance of an object. ( FirstPersonController)
Hi, I Here's a part of my edited FirstPersonController script:
public void Update()
{
var hud = GetComponent<HUD>();
if (hud.Inventory.enabled == true || hud.Pause.enabled == true)
{
m_MouseLook.XSensitivity = 0f;
m_MouseLook.YSensitivity = 0f;
}
}
When I start a game an error is displaying, How to fix it?
PS: Error in 4th line of code.
Comment
Best Answer
Answer by moltow · Aug 06, 2017 at 08:29 PM
So hud
is likely null. Is the HUD
component attached to the same GameObject
to which this script is attached?
Thank you, HUD script was attached to FirstPersonCharacter and this script was attached to FPSController.
Great! Glad I could help...
One thing I didn't notice before posting...
I wouldn't use GetComponent<HUD>()
every frame in Update()
. Ins$$anonymous$$d, I'd get it in Start()
or Awake()
Something like:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Chase : $$anonymous$$onoBehaviour {
HUD hud;
void Start ()
{
hud = GetComponent<HUD>();
}
public void Update()
{
if (hud == null)
{
Debug.Log("hud is null. Drat!");
return;
}
if (hud.Inventory.enabled == true || hud.Pause.enabled == true)
{
m_$$anonymous$$ouseLook.XSensitivity = 0f;
m_$$anonymous$$ouseLook.YSensitivity = 0f;
}
}
}