Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by DudeBrainz · Oct 13, 2016 at 11:00 PM · c#coroutineshootingwaitforsecondsenemyai

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)

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DudeBrainz · Oct 13, 2016 at 11:57 PM 0
Share

please help

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;
 
     }

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DudeBrainz · Oct 14, 2016 at 09:17 PM 0
Share

lfs means another trigger collider is used to see if a player is shootable

avatar image DudeBrainz · Oct 14, 2016 at 09:18 PM 0
Share

and i think you fixed it

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

228 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Why isn't my simple coroutine working? (and how can I make it infinite?) 2 Answers

What is wrong with this use of WaitForSeconds? 1 Answer

WaitForSeconds behaves weirdly 1 Answer

Can't get past WaitForSeconds in my coroutine 1 Answer

Playing footsteps with interval 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges