Trying to Invoke method: Laser.Canfire couldn't be called.

Hey guys!

I’m busy following a space shooter tutorial on YouTube and pretty much following the guy’s steps verbatim. However, I’m experiencing an issue in my Laser.cs script that the guy on YouTube (older version of Unity) doesn’t seem to be experiencing…

My exact issue came in when I started adding fireDelay, which is supposed to add a 2 second delay between my laser shots, but in actuality is causing my laser to only fire once and never again. The error in Unity suggests that the “Invoke” method can’t be called in the script… Any help would mean a lot!

I’m super new to this - this is my first time scripting. Please be gentle with me! XD

Here is the whole script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent (typeof(Light))]
[RequireComponent(typeof(LineRenderer))]
public class Laser : MonoBehaviour {
  [SerializeField]float LaserOffTime = .5f;
  [SerializeField]float maxDistance = 300f;
  [SerializeField]float fireDelay = 2f;
  LineRenderer lr;
  Light laserLight;
  bool canFire;

  void Awake()
  {
    lr = GetComponent<LineRenderer>();
    laserLight = GetComponent<Light>();
  }

  void Start()
  {
    lr.enabled = false;
    laserLight.enabled = false;
    canFire = true;
  }

  public void FireLaser(Vector3 targetPosition)
  {
    if(canFire)
    {
      lr.SetPosition(0, transform.position);
      lr.SetPosition(1, targetPosition);
      lr.enabled = true;
      laserLight.enabled = true;
      canFire = false;
      Invoke("TurnOffLaser", LaserOffTime);
      Invoke("Canfire", fireDelay);
    }
  }

  void TurnOffLaser()
  {
    lr.enabled = false;
    laserLight.enabled = false;
  }

  public float Distance
  {
    get { return maxDistance; }
  }

  void CanFire()
  {
    canFire = true;
  }
}

Capitalization matters. Canfire is not the same as CanFire

Hey thanks for the quick reply!

Yeah I’m aware of the case sensitivity, but I must be blind because I’ve combed through my code and I can’t find the mistake anywhere! My eye isn’t tuned in to this yet…

EDIT: facepalm. I’ve found it. This is embarrassing.

1 Like

Nah, no need… we’re all human… I’ve been doing this for 41 years and I still get capitalization wrong. Just learn to cross-check things like capitalization, spelling, punctuation, syntax, etc. as you go, and you’ll gradually increase your arsenal of things to check whenever you see a new and unfamiliar error.

1 Like

Incidentally, this problem is good reason to learn the alternative techniques to anything that uses a string to call code like this. (such as Invoke, SendMessage, BroadcastMessage, and the string version of StartCoroutine) It doesn’t autocomplete, when you get it wrong it doesn’t give you a compile-time error, and they also perform worse.

Here’s an example of how you might replace your code with something that doesn’t use a string:

//instead of
      Invoke("CanFire", fireDelay);

//use
      StartCoroutine(WaitThenCanFire(fireDelay));

//and elsewhere
IEnumerator WaitThenCanFire(float delay) {
    yield return new WaitForSeconds(delay);
    canFire = true;
}
3 Likes

I’ll definitely be trying this. Thanks for your helpful support!