PlayerScaling issues with transform.localscale

I’ve seen in many similair forums that people use transform.localscale for transforming the size of their character. I have a 2D sprite with a circlecollider2D, rigidbody2D, PlayerScript, and another circle collider for checking.

The script that’s having an issue is:

//Created by Joel Draper for MansionGaming.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class HealthSystem : MonoBehaviour
{

    public LevelManager levelManager;

    public RespawnManager respawn;

    private int health;

    private bool done = false;

    public bool isPlayer = false;

    Text text;

    void Start()
    {
        text = GetComponent<Text>();

        health = 100;

        text.text = " " + health;
    }

    public void resetHealth()
    {
        health = 100;
    }

    void Update()
    {
        if (health <= 0)
        {
            levelManager.RespawnPlayer();
            respawn.deathRespawn();
            health = 100;
        }

        text.text = health.ToString();
        if (health >= 100 && isPlayer == true)
        {
            transform.localScale = new Vector3(20f, 20f, 20f);
        }
        if (health < 100 && isPlayer == true)
        {
            transform.localScale = new Vector3(10f, 10f, 1f);
        }
        if (health < 80 && isPlayer == true)
        {
            transform.localScale = new Vector3(3f, 3f, 3f);
        }

        if (done == false)
        {
            if (Input.GetMouseButtonDown(0))
            {
                StartCoroutine("HealthDamage");
                done = true;
            }
        }
    }

    public void cookieHealth()
    {
        health += 25;
    }

    public void spikeDamage()
    {
        health -= 5;
    }

    public IEnumerator HealthDamage()
    {
        while (true)
        {
            health -= 1;
            yield return new WaitForSeconds(1);

        }
    }
}

If I set the UI element which I’m using to check the change in health, as a player - it changes in size, but the play doesn’t. Does anyone know how to fix it?

Works for me (just had to remove those “text.text = " " + health;” and “text.text = health.ToString();” since my sprite doesnt have text component…)
Are you not getting any error messages?

Which I’m not so sure why.

Write a Debug.Log to see if GetComponent< Text >() actually find any text, it could be causing the problem, aswell as if you’ve forgotten to assign a level manager or respawn manager reference in the scene. :slight_smile:

It finds the text fine, as it updates the text - it’s just the player size.

Level Manager and Respawn Manager are also assigned.

Update: It’s something to do with the health script, as now you don’t lose health on entering an area that removes health, while previously you could. Although the health UI still updates as it should.

Update2: Someone’s helped me to make the player size work, but now you can’t add or remove health. >.<