- Home /
The question is answered, right answer was accepted
null reference but component shows up in the inspector.
I am getting null reference exception
when debugging. It returns the component and then after says it is null. I ran it in update and it just goes back and forth between the component and null
[[url=https://postimg.org/image/rpvku7z29/][img]https://s16.postimg.org/rpvku7z29/cap2.png[/img][/url]
https://s16.postimg.org/yhlzx2o1x/Capture.png ][1] << link to images to show you what im getting. Its saying that the playermove is null. I debugged it. and even initialized it at the point of the error if it returned null. it keeps saying its null and not working. the variables inside the script are public as well.
public class PlayerMeleeManager : MonoBehaviour {
public PlayerMovement playerMove;
public PlayerManager playerManager;
int maxCombo;
public int attIndex;
int attackState;
bool targetHit;
public bool lockedOn;
bool comboTriggered;
public bool attacking = false;
private void Start()
{
if (GetComponent<PlayerMovement>())
{
playerMove = GetComponent<PlayerMovement>();
}
else
{
Debug.Log("cant find it");
}
playerManager = GetComponent<PlayerManager>();
InitializeAttacks();
InitializeVaraibles();
}
private void Update()
{
CheckForInput();
}
void InitializeVaraibles()
{
//maxCombo = AttackListManager.meleeAttackOrder.Count;
attacking = false;
targetHit = false;
comboTriggered = false;
}
void CheckForInput()
{
if (Input.GetKeyDown(KeyCode.Z))
{
Attack();
}
}
void InitializeAttacks()
{
AttackListManager.InitializeAbilities();
AttackListManager.RegulateAttacks(AttackListManager.meleeAttackOrder, AttackListManager.regularGroundComboList);
}
public void Attack()
{
if (!attacking)
{
playerMove.playerState = PlayerMovement.PlayerAnimationState.ATTACKING;
attIndex = 0;
attacking = true;
}
else if (attacking && targetHit && !comboTriggered)
{
comboTriggered = true;
}
}
public void ResetAttack()
{
if (comboTriggered)
{
targetHit = false;
comboTriggered = false;
attIndex++;
}
else
{
playerMove.playerState = PlayerMovement.PlayerAnimationState.IDLE;
attIndex = 0;
attacking = false;
}
}
public void ToggleDamage(bool isOn)
{
playerManager.playerWeapon.GetComponent<CapsuleCollider>().enabled = isOn;
if (!isOn)
{
}
}
public void StepForward(float x)
{
if (lockedOn)
{
playerManager.playerRigidbody.AddForce(Vector3.zero, ForceMode.VelocityChange);
playerManager.playerRigidbody.AddForce(transform.forward * x * 3, ForceMode.Impulse);
}
else
{
Vector3 dir = PlayerTargetManager.playerTarget.position - transform.position;
dir = dir.normalized;
playerManager.playerRigidbody.AddForce(Vector3.zero, ForceMode.VelocityChange);
playerManager.playerRigidbody.AddForce(dir * x * 3, ForceMode.Impulse);
}
}
}
Why does it say this has been closed with an accepted answer? Doesn't seem to have any answers at all, and seems like a quite valid question. I'm experiencing the same where I'm setting a reference on a newly spawned gameobject's component through an Init method. The reference is visible in the inspector, but when accessing it through script I get a null reference. Probably the same issue?
Follow this Question
Related Questions
Object reference is NULL when IT IS set to an instance of an object? 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
The type or namespace name `Events' does not exist in the namespace `TouchScript' 1 Answer