Search Unity

Question SOLVED: Beginner issue - Broken Coroutine.

Discussion in 'Scripting' started by Henzig, Oct 27, 2020.

  1. Henzig

    Henzig

    Joined:
    Aug 18, 2020
    Posts:
    22
    Hey guys.

    Noob at a loss... Can you see an obvious error here? I sure can't...

    Following script is written to loop a coroutine (row 49) at getMouseButtonDown (row 31). The coroutine never loops however.

    Remarks:
    - "Target" is not assigned in the event that there's no collider hit. Is that why the coroutine stops?
    - Another suspect is the "gatlinFireRate" which is a late edition.
    - The RayCast is proven and seems to work. The issue lies in the if-statements.
    - The code never reaches the debug at row 72. Not even once.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class bollabotShoot : MonoBehaviour
    6. {
    7.     fireGatlinScript fireGatlinScript;
    8.     fireGrenadeScript fireGrenadeScript;
    9.  
    10.     private Vector3 aimPos;
    11.     private Vector3 aimDirection;
    12.     private GameObject gatlinSpawnPoint;
    13.     private float gatlinFireRate = 0.2f;
    14.     private bool gatlinReady = true;
    15.  
    16.     public GameObject target;
    17.     public bool onslaughtMode = false;
    18.  
    19.     void Start()
    20.     {
    21.         fireGatlinScript = GetComponent<fireGatlinScript>();
    22.         fireGrenadeScript = GetComponent<fireGrenadeScript>();
    23.         gatlinSpawnPoint = GameObject.Find("gatlinSpawnPoint");
    24.     }
    25.  
    26.     void Update()
    27.     {
    28.         aimPos = GetComponent<playerLook>().aimPos;
    29.         aimDirection = aimPos - gatlinSpawnPoint.transform.position;
    30.  
    31.         if (Input.GetMouseButton(0) && gatlinReady)
    32.         {
    33.             gatlinReady = false;
    34.             StartCoroutine(gatlinInput());
    35.             Debug.Log("getMouseButtonDown");
    36.         }
    37.  
    38.         if (Input.GetMouseButton(1))
    39.         {
    40.             fireGrenadeScript.grenadeTrajectory();
    41.         }
    42.  
    43.         else if (Input.GetMouseButtonUp(1))
    44.         {
    45.             fireGrenadeScript.fireGrenade();
    46.         }
    47.     }
    48.  
    49.     private IEnumerator gatlinInput()
    50.     {
    51.         RaycastHit hit;
    52.         Ray shootRay = new Ray(gatlinSpawnPoint.transform.position, aimDirection);
    53.         Debug.DrawLine(gatlinSpawnPoint.transform.position, aimPos, Color.red, 0.2f);
    54.  
    55.         if (Physics.Raycast(shootRay, out hit))
    56.         {
    57.             target = hit.collider.gameObject;
    58.  
    59.             if (onslaughtMode)
    60.             {
    61.                 aimPos = hit.collider.ClosestPoint(this.transform.position);
    62.             }
    63.  
    64.             fireGatlinScript.fireGatlinHit();
    65.         }
    66.         else if (!Physics.Raycast(shootRay, out hit))
    67.         {
    68.             fireGatlinScript.fireGatlinHitNull();
    69.         }
    70.         yield return new WaitForSeconds(gatlinFireRate);
    71.         gatlinReady = true;
    72.         Debug.Log("gatlinInput");
    73.     }
    74. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,914
    Is
    Debug.Log("getMouseButtonDown");
    ever printing to your console?
    Are there any errors in the console?
     
  3. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Just curious.. is your debug line being drawn? I would think that setting gatlinready to false before starting the coroutine is cancelling out your if statement.
     
    Last edited: Oct 27, 2020
  4. Henzig

    Henzig

    Joined:
    Aug 18, 2020
    Posts:
    22
    Good morning fellas. Thanks for helping me out.

    -”getMouseButton” is printed but only once. No errors.

    The debug line is inconstistent. I belive it prints for one frame making it fast enough not to be visible every time. In short - I believe so, but also only once.
     
  5. Madgvox

    Madgvox

    Joined:
    Apr 13, 2014
    Posts:
    1,317
    Are you sure that there are no errors? Make sure that errors aren't being hidden by the console.

    If "gatlinInput" isn't being printed then something is erroring inside the coroutine.
     
  6. Henzig

    Henzig

    Joined:
    Aug 18, 2020
    Posts:
    22
    Yep the issue is in the coroutine...

    I’ll dubbelcheck when I get back home (Swedish times).

    Thanks.
     
  7. Boo-Let

    Boo-Let

    Joined:
    Jan 21, 2019
    Posts:
    150
    Another thing... I’ve been looking at this and thinking..

    I don’t really recommend using a coroutine for this. Though it may work, at some point. I don’t see it really going to perform the way you expect. If you want this to work this way, stick a debug.log statement into each if statement and see how far you get. Then you’ll know how far you are getting before something is “breaking”.

    Any ways all that aside, I would look into using interfaces. One for your weapon one for a target. Say maybe “IWeapon” and “IDamageable”. Then with the if mouse button code, call the weapon interface to start attacking, and if it hits something with IDamageable, then call that target to take damage. I would study into interfaces as it’ll give you a more broad range of control without duplicating code for weapon and save your code to use that weapon.

    There are a lot of good tutorials online about them.

    This one should set you in a good running motion

    https://m.youtube.com/watch?v=kYJRIWjoeFA
     
    Last edited: Oct 28, 2020
  8. Henzig

    Henzig

    Joined:
    Aug 18, 2020
    Posts:
    22
    Alright I'm back. And thank you all SOOO much for lending an eye.

    My structure seems to be double broken...

    @Boo-Let You are right of course. I've tried to separate the functions in different scripts but I'm starting to learn that these things have a life of their own and if not held on a short leash...
    @Madgvox No errors other than "target" not being assigned in the event of a collider. I still thinks that's the culprit.

    I think I might know what the issue is. I'll have to rebuild some of this to whip it back into shape and get back to you. Might take a day or two though... :)

    Have a great one!
     
  9. Henzig

    Henzig

    Joined:
    Aug 18, 2020
    Posts:
    22
    Yep. Double coroutines and a poorly written if-statement seems to be the issue here.

    Gonna put i some time to re-write the structure using interfaces as suggested. It looks like a better solution.

    Thanks again!
     
    Madgvox likes this.