0

I have 2 images (2 players), these will be refilled each time a player fires (working as a reload animation). Here is the code for the Reload class

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

public class Timer : MonoBehaviour {

public Image fillImg1;
public Image fillImg2;
float timeAmt = 1;
float time;

void Start() { 
   // GameObject RC = GameObject.Find("ReloadCanvas");
    fillImg2 = GetComponent<Image>();
    time = timeAmt;
}

public void Update() {

    if (Input.GetKeyDown("space"))
    {
        if (fillImg2 != null)
        {
            Debug.Log("Got to P2 Reload");
            while (time > 0)
            {
                time -= Time.deltaTime;
                fillImg2.fillAmount = time / timeAmt;
            }

            Debug.Log("Time reset - " + Time.deltaTime);
            time = timeAmt;
        }
        else { Debug.Log("Fill Image 2 is null"); }
    }

    if (Input.GetKeyDown(KeyCode.LeftShift))
    {
        if (fillImg2 != null)
        {
            Debug.Log("Got to P1 Reload");
        while (time > 0)
        {
            time -= Time.deltaTime;
            fillImg1.fillAmount = time / timeAmt;
        }

        Debug.Log("Time reset - " + Time.deltaTime);
        time = timeAmt;

    }
    else { Debug.Log("Fill Image 1 is null"); }
}
}
}

My problem is that even though i have the images filled in the canvas, the 'else if the images are null' check are being triggered.

Here is an image of the ReloadCanvas

1 Answer 1

1

The issue is that on Start() you are pointing your fillImg2 image to a null. When you use GetComponent<>() you are checking the component of a GameObject which your fillImg2 is already an image.

Remove fillImg2 = GetComponent<Image>(); from your Start method and it should no longer be null.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks! The errors are no longer appearing, however now the images are disappearing when I fire for some reason
Thats because your setting the image to fill from 1 to 0 causing the image to disappear. To have the image fill up upon firing you will need to set time = 0 then reverse the time -= to time +=. This will make the image disappear immediately after firing then fill up from 0 to 1.
Tried it there and it isnt changing the image at all for some reason. My current code is if (fillImg2 != null) { Debug.Log("Got to P2 Reload"); time2 = 0; while (time2 <= timeAmt2) { time2 += Time.deltaTime; fillImg2.fillAmount = time2; } time2 = timeAmt2; }
Sorry for the late response. Not exactly sure about that one. I would suggest checking your image type in the inspector to see if its set to filled. If that's not set it won't change.
No worries, had to start a different thread for that issue and got it resolved. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.