- Home /
my script is broken again(enemy refuses to stop seeing me)
using UnityEngine;
using System.Collections;
public class enemy : MonoBehaviour {
[SerializeField]
private float CrabSpeed;
[SerializeField]
private GameObject Projectile;
[SerializeField]
private Collider2D player;
[SerializeField]
private LookForShootables lfs;
[SerializeField]
private Collider2D radar;
Quaternion zrotate;
bool lookingright;
bool SeePlayer;
bool SeePlayerShootable;
bool Triggered;
private float TTN;
[SerializeField]
private float MTTN;
private float r = 0;
private float t = 0;
// Use this for initialization
IEnumerator TimeToNeutral()
{
yield return new WaitForSeconds(3);
Debug.Log("bye");
Triggered = false;
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Player")
{
Debug.Log("holy crap!");
SeePlayer = true;
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "Player")
{
Debug.Log("whereDgo");
SeePlayer = false;
}
}
void FixedUpdate()
{
SeePlayerShootable = lfs.shootables;
//--------------------------------------
if (lookingright == !false)
transform.localScale = new Vector3(-3, 3, 1);
if (lookingright == !true)
transform.localScale = new Vector3(3, 3, 1);
//thesearedebugs
if (Input.GetKey(KeyCode.H))
lookingright = true;
if (Input.GetKey(KeyCode.G))
lookingright = false;
if (r > 0)
r = r - 1;
//*****************************************************************
//-----------------------------------------------------------------
//*****************************************************************
if (SeePlayer == true && r == 0)
{
Triggered = true;
Debug.Log("triggered!!! See player");
StopCoroutine("TimeToNeutral");
r = 1;
}
if (SeePlayerShootable == true && t == 0)
{
StartCoroutine("shoot");
t = 1;
}
if (SeePlayerShootable == false && t == 1)
{
StartCoroutine("shoot");
t = 0;
}
if (SeePlayer == false && r == 1)
{
StopCoroutine("shoot");
StartCoroutine("TimeToNeutral");
r = 0;
}
}
IEnumerator shoot()
{
Debug.Log("shooting from corutine shoot");
if (lookingright == true)
zrotate = new Quaternion(0, 0, 180, 0);
if (lookingright == false)
zrotate = new Quaternion(0, 0, 0, 0);
Rigidbody bullet;
bullet = Instantiate(Projectile, transform.position, zrotate) as Rigidbody;
bullet.AddForce(transform.forward * 30);
yield return new WaitForSeconds(3);
t = 0;
}
}
so. I have this script. my enemy refuses to stop seeing me. It's probably a problem with the coroutines. so here's what happens: 1. my enemy fires one bullet, refuses to fire moar. 2. my enemy debugs "triggered" every frame 3. my enemy loses sight of me and acknowledges it, but wont run time to neutral and debug "bye".
and on a side note, It spawns the bullet but gives me the error: NullReferenceException: Object reference not set to an instance of an object enemy+c__Iterator2.MoveNext () (at Assets/Art/enemys/crab/crabScripts/enemy.cs:116) UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17) UnityEngine.MonoBehaviour:StartCoroutine(String) enemy:FixedUpdate() (at Assets/josef art/enemys/crab/crabScripts/enemy.cs:90)
Answer by m1le · Oct 14, 2016 at 05:54 AM
Ok i tried to understand what you want.
// This coroutine, we can erase it see later...
IEnumerator TimeToNeutral()
{
yield return new WaitForSeconds(3);
Debug.Log("bye");
Triggered = false;
}
// OK keepin' this i understand i see you or not see you
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Player")
{
Debug.Log("holy crap!");
SeePlayer = true;
}
}
void OnTriggerExit2D(Collider2D col)
{
if (col.tag == "Player")
{
Debug.Log("whereDgo");
SeePlayer = false;
}
}
// Problems comes
void FixedUpdate()
{
// What this lfs.shootables means, you don't tell us
SeePlayerShootable = lfs.shootables;
// these are work for your debug!!!
if (lookingright == !false) // It's not false so it's true
transform.localScale = new Vector3(-3, 3, 1);
if (lookingright == !true)
transform.localScale = new Vector3(3, 3, 1);
//thesearedebugs
if (Input.GetKey(KeyCode.H))
lookingright = true;
if (Input.GetKey(KeyCode.G))
lookingright = false;
// Just do
if (Input.GetKey(KeyCode.H))
lookingright = true;
transform.localScale = new Vector3(-3, 3, 1);
if (Input.GetKey(KeyCode.G))
lookingright = false;
transform.localScale = new Vector3(3, 3, 1);
// These r and t variable i really don't understand what they are suppose to do ?
if (r > 0)
r = r - 1;
//*****************************************************************
//-----------------------------------------------------------------
//*****************************************************************
// What is the way beetween I see you and i can shoot you ???
if (SeePlayer == true && r == 0)
{
Triggered = true;
Debug.Log("triggered!!! See player");
StopCoroutine("TimeToNeutral");
r = 1;
}
if (SeePlayerShootable == true && t == 0)
{
StartCoroutine("shoot");
t = 1;
}
if (SeePlayerShootable == false && t == 1)
{
StartCoroutine("shoot");
t = 0;
}
if (SeePlayer == false && r == 1)
{
StopCoroutine("shoot");
StartCoroutine("TimeToNeutral");
r = 0;
}
}
IEnumerator shoot()
{
Debug.Log("shooting from corutine shoot");
zrotate = lookingright ? new Quaternion(0, 0, 180, 0) : new Quaternion(0, 0, 0, 0);
// Create a game object bullet and place it in a resources folder in your project
// While seeplayer is true, the ennemy fire a bullet every 3 seconds
while (SeePlayer) {
GameObject bullet = Instantiate(Resources.Load("bullet", typeof(GameObject)),transform.position,zrotate) as GameObject;
bullet.GetComponent<Rigidbody2D>().AddForce(transform.forward * 30);
yield return new WaitForSeconds(3);
}
// Seeplayer is not true anymore, the code write after this run so you can place TimeToNeutral here
yield return new WaitForSeconds(3);
Debug.Log("bye");
Triggered = false;
}
lfs means another trigger collider is used to see if a player is shootable