Witam chce stowrzyć strzelającego przeciwnika w Unity ale wyskakuje mi taki bład jak to rozwiązać ?
NullReferenceException: Object reference not set to an instance of an object
Enemy.Enemy_Bullet.Awake () (at Assets/Enemy_Bullet.cs:18)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace Enemy
{
public class Enemy_Bullet : MonoBehaviour
{
public float timebtwBullets = 0.15f;
public GameObject projectile;
float nextBullet;
Enemy_Controller EC;
bool RamboMode;
void Awake()
{
nextBullet = 0f;
RamboMode= EC.detected;
}
void Update()
{
Enemy_Controller myEnemy = transform.root.GetComponent<Enemy_Controller>();
if (RamboMode)
{
nextBullet = Time.time + timebtwBullets;
Vector3 rot;
if (myEnemy.GetFacing() == -1f)
{
rot = new Vector3(0, -90, 0);
}
else
rot = new Vector3(0, 90, 0);
Instantiate(projectile, transform.position, Quaternion.Euler(rot));
}
}
}
}
2 kod z definicją do detected
namespace Enemy
{
public class Enemy_Controller : MonoBehaviour
{
public GameObject flipModel;
public GameObject FollowPlayer;
public float runSpeed = 5f;
public bool Playerdetected;
public bool Facingright = true;
public bool detected;
public float detectionTime;
float startRun;
public bool attackPlayer;
public Transform detectedPlayer;
float moveSpeed;
public float walkSpeed;
public float attackdistance = 2f;
Rigidbody myRB;
Animator EnemyAnim;
public bool Attack;
bool run;
public bool inRange;
bool death;
void Start()
{
myRB = GetComponent<Rigidbody>();
EnemyAnim = GetComponent<Animator>();
Facingright = true;
moveSpeed = walkSpeed;
if (Random.Range(0, 10) > 2) Flip();
}
void Update()
{
//Animator Bools definition
if (Attack == false)
EnemyAnim.SetBool("attack", false);
if (Attack == true)
EnemyAnim.SetBool("attack", true);
if (run == false)
EnemyAnim.SetBool("run", false);
if (run == true)
EnemyAnim.SetBool("run", true);
if (inRange == false)
EnemyAnim.SetBool("inRange", false);
if (inRange == true)
EnemyAnim.SetBool("inRange", true);
}
void FixedUpdate()
{
if (detected)
{
if (detectedPlayer.position.x < transform.position.x && Facingright) Flip();
else
if (detectedPlayer.position.x > transform.position.x && !Facingright) Flip();
{
if (detected && Facingright) myRB.velocity = new Vector3(moveSpeed, myRB.velocity.y, 0);
if (detected && !Facingright) myRB.velocity = new Vector3(moveSpeed * -1, myRB.velocity.y, 0);
if (detected)
{
startRun = Time.time + detectionTime;
}
}
// FollowTarget();
if (!detected)
return;
if (detected == true)
{
if (Vector3.Distance(transform.position, detectedPlayer.position) > attackdistance)
{
moveSpeed = walkSpeed;
Attack = false;
run = true;
attackPlayer = false;
if (detected && Facingright) myRB.velocity = new Vector3(moveSpeed, myRB.velocity.y, 0);
if (detected && !Facingright) myRB.velocity = new Vector3(moveSpeed * -1, myRB.velocity.y, 0);
}
else if (Vector3.Distance(transform.position, detectedPlayer.position) <= attackdistance)
{
myRB.velocity = Vector3.zero;
moveSpeed = 0;
attackPlayer = true;
Attack = true;
run = false;
}
}
}
}
void OnTriggerEnter(Collider other)
{
if (other.tag == "Player")
{
inRange = true;
detected = true;
detectedPlayer = other.transform;
if (detectedPlayer.position.x < transform.position.x && Facingright) Flip();
else
if (detectedPlayer.position.x > transform.position.x && !Facingright) Flip();
}
}
void OnTriggerExit(Collider other)
{
if (other.tag == "Player")
{
detected = false;
run = false;
inRange = false;
}
}
void Flip()
{
Facingright = !Facingright;
Vector3 theScale = flipModel.transform.localScale;
theScale.z *= -1;
flipModel.transform.localScale = theScale;
}
public float GetFacing()
{
if (Facingright) return 1;
else return -1;
}