- Home /
Question by
ElysiumGames · Nov 26, 2017 at 05:02 PM ·
programmingcooldownaccessing scripts
Accessing a script on another GameObject
I am attempting to call a function from another script on another GameObject, but the function isn't being called at all. There are no errors being thrown and I'm honestly stumped as to why it isn't working. The GameObject is definitely correctly named too. Any advice would be much appreciated. Thanks.
Trying to call the function, all code around this works perfectly
GameObject.Find("WeaponCooldown").GetComponent<AbilityCooldown>().ShotFired();
The script that contains the function I am trying to call
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class AbilityCooldown : MonoBehaviour {
public Image cooldown;
public bool coolingDown;
private float cooldownTime;
void Start()
{
cooldownTime = GameObject.Find("Player").GetComponent<Shooting>().cooldownTime;
cooldown.fillAmount = 0f;
Debug.Log("Cooldown time found.");
}
public void ShotFired() {
Debug.Log("Shot fired.");
cooldown.fillAmount = 0f;
coolingDown = true;
}
void Update () {
if (coolingDown)
{
Debug.Log("Cooling down");
cooldown.fillAmount += 1f / cooldownTime * Time.deltaTime;
if (cooldown.fillAmount == 1f)
coolingDown = false;
}
}
}
Comment
Hi, The scripts seems to be fine. Please make sure the game object name ("WeaponCooldown") is spelled correct and also it has the "AbilityCooldown" script attached. you can also try this to see if you are getting a proper reference to an object with the "AbilityCooldown" script attached.
GameObject.FindObjectOfType<AbilityCooldown>().ShotFired();