- Home /
Object Instantiate Image to Canvas, Object destroy, Image destroy?
Using C# in Unity 5.6...
I have an Object 'player' who creates an Image 'healthBar' to the Canvas. I can sucessfully change 'healthBar' scale, and have it's position move with 'player'. But when I wish for 'player' to die 'healthbar' stays in the canvas. I can't get it's correct InstanceID to destroy the correct 'healthbar' in the Canvas...
I only posted relevant code
What am I doing wrong, or what should I be doing?
public class Player : MonoBehaviour {
public Canvas canvas;
private Image createHealthbar;
public Image healthBarImage;
private Image healthBar;
private Image createHealth;
public Image healthImage;
private Image health;
void Start(){
pixelX = canvas.worldCamera.pixelWidth;
pixelY = canvas.worldCamera.pixelHeight;
}
void Update(){
if (AmSelected == true) { IsSelected (); }
if (Input.GetKeyUp (KeyCode.H)) {
if (health.transform.localScale.x > 0.05f) {
health.transform.localScale -= new Vector3 (.1f, 0f, 0f);
health.transform.localPosition -= new Vector3 (5f, 0f, 0f);
} else {
//Destroy (createHealthbar); //These don't work...
//Destroy (createHealth); //These don't work...
Destroy(gameObject); //Player destroys but healthbars remain...
}
}
}
void IsSelected(){
if (healthBar != null) {
Vector3 screenPos = Camera.main.WorldToScreenPoint (this.transform.position + heightOffset);
healthBar.transform.localPosition = (new Vector3 (screenPos.x - (pixelX / 2f), screenPos.y - (pixelY / 2f), 0f));
Debug.Log ("itID " + healthBar.GetInstanceID());
//Debug shows a different ID, no matter which (healthBarImage, createHealthbar, healthBar) I use...
} else {
createHealthbar = Instantiate(healthBarImage, canvas.transform) as Image;
healthBar = createHealthbar;
createHealth = Instantiate (healthImage, createHealthbar.transform);
health = createHealth;
}
}
}
Answer by CoryButler · Jul 09, 2017 at 05:10 AM
I'm a bit confused by what appear to be redundant Images. So I'm not sure which one you need to Destroy().
Try destroying the related gameObject, and not just the Image component. For example:
Destroy (createHealthbar.gameObject);
wow... mother of nuggets, it really WAS that simple....
sorry, I was racking my brain all day, and hit that roadblock, and tapped out....
3 images, yes, one being the actual prefab, one being the instantiate, and the last to actually control/get it moving.... I simplified it a tad by:
Image createHealthbar = Instantiate(healthBarImage, canvas.transform) as Image;
getting rid of the second declare... But yes:
Destroy(healthBar.gameObject);
also destroys the Image in the canvas hierarchy! Thank you so much!
Your answer
Follow this Question
Related Questions
UI Instantiate?,Ui, Sumonar UI? 0 Answers
Can you Instantiate a Canvas into a scene? (Not objects inside of a canvas, but the canvas itself) 0 Answers
How do I change scene with two ui canvas text 2 Answers
Disable and enable canvas group by pressing an UI button. (C#) 2 Answers
Close UI using Canvas Group 1 Answer