Why doesnt the value change?
Im trying to break a block but the value in BlockData doesnt change.
(on all blocks)
public class BlockData : MonoBehaviour {
public float health;
public float strength;
}
This is called everytime i click a button, the Debug ges called but the health doesnt change. A block who has 1 health gets instant destroyed but a block with 5 health never dies... Why desnt this work?
void BreakBlock()
{
RaycastHit hit;
if (Physics.Raycast(cam.transform.position, cam.transform.forward, out hit, range, LayerMask.GetMask("Breakable")))
{
float health = hit.transform.gameObject.GetComponent<BlockData>().health;
float strength = hit.transform.gameObject.GetComponent<BlockData>().strength;
if (breakingStrength > strength)
{
Debug.Log("attack");
health = health - blockDamage;
if(health < 1)
{
Destroy(hit.transform.gameObject);
}
}
}
}
}
Answer by filifjonkaren · Jun 17, 2017 at 12:51 PM
I made a small change and it works now. Thx for help!
Answer by AtGfx · Jun 17, 2017 at 09:46 AM
Because you never reassign the health to the gameobject hit. Your health variable is only set locally (so if health=1, gameobject is destroyed, else it is never updated).
Hope it helps ;)
Your answer
Follow this Question
Related Questions
Display GameObjects tag using Raycast 1 Answer
How can i place subjects in different positions? 0 Answers
RayCast and Animation not working 0 Answers
Scripting Errors 1 Answer