- Home /
i cant drag my image in the inspector when i use the code public image how can i fix this
this is my code:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class healthsystem : MonoBehaviour { private int health; public int health_max; // public if you want to drag your text object in there manually
the part here doesn't work so yeah how can i fix this public Image bar;
private void Start()
{
health = health_max;
}
void Update()
{
if (health <= 0)
{
Destroy(gameObject);
}
}
public void TakeDamage(int damage)
{
health -= damage;
bar.fillAmount = health / health_max;
//bar.localScale = new Vector3(1/(health/damage)f, 1f));
}
}
any suggestions?
Answer by Orami · Mar 09, 2019 at 09:03 PM
int can not hold an image it is just a number. You need it to be an image. You don't even have a variable set up to hold the image in the code you posted. Oh wait there it is you need to click code instead of just pasting into this editor.
Try Sprite instead of Image.
when i use sprite it says an error that i need an assembly reference but then still in the inspector no thing comes where you can drag and drop the object in so i have no idea why.
(and thanks for helping tho)
Your answer
Follow this Question
Related Questions
is there a way to change the public inspector details at runtime 1 Answer
Public string/GameObject not in inspector 1 Answer
How to set a public variable to a gameObject in the inspector through coding 1 Answer
How to check if a public method is referenced in inspector 3 Answers
Implementing an Image Gallery 4 Answers