- Home /
Question by
dragonking300 · Jul 17, 2017 at 04:19 PM ·
c#error messageparent-child
My dagger attached to an enemy won't detect it's parent's Ai Script
ERROR: NullRefrenceException: Object Refrence not set as an instance of an object DaggersScript.Update() (at Assets/DaggersScript.cs:34)
So I checked line 34 and checked all of the names and set all of the types to public and it still won't detect the Ai script attached to its parent, and no, I can't manually assign it through the inspector because the enemy is a prefab
using UnityEngine;
using System.Collections;
public class DaggersScript : MonoBehaviour {
private GameObject Player;
private bool Attacking;
private bool Charging;
private int TimeToTillAttack;
private bool ChargingAtPlayer;
private Transform DaggerPosition;
private GameObject Dagger;
public GameObject Enemy;
public BasicEnemyAi BasicAi;
// private float Timer;
private Animator animate;
public AnimationClip ChargeUp;
public AnimationClip Attack;
void Start ()
{
animate = GetComponent<Animator>();
Player = GameObject.FindGameObjectWithTag("Player");
Enemy = transform.parent.gameObject;
DaggerPosition = gameObject.transform.GetChild(0);
Dagger = transform.parent.gameObject;
BasicEnemyAi BasicAi = Enemy.GetComponent<BasicEnemyAi>();
// Timer = Attack.length;
// Attack.length = 0;
}
// Update is called once per frame
void Update ()
{
if(BasicAi.Timer <= 0)
{
Charging = false;
ChargingAtPlayer = true;
return;
}
else
{
Charging = true;
ChargingAtPlayer = false;
}
}
}
Comment
have u checked if the object u set as Enemy has the BasicAi script?
Best Answer
Answer by mefsh · Jul 17, 2017 at 06:47 PM
Your declaring BasicAi twice.
Change Line 26
BasicEnemyAi BasicAi = Enemy.GetComponent<BasicEnemyAi>();
To
BasicAi = Enemy.GetComponent<BasicEnemyAi>();
That's all I can think of. Hopefully it helps.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Unloading Broken Assembly | How to solve this !? 2 Answers