Scale of object does not update properly with public variable
Hello,
I am trying to build a mechanic for my game where there is a light source around the player that slowly shrinks overtime. I have built min and max light to set the size within a certain range. Everything is working properly so far, except for when I want to change the variables float value. The scale is targeted properly and shrinks, but Unity remembers the initial value I typed into the variable for some reason. Even if I change the variable value in the script, save the script and reset it nothing happens. The only way it changes is when I rename the variable. I have checked in every other script to see if I am referencing it, but I am not. The only place the scale of that object is being changed is in the light script. So once new the minimum size is hit, the object's scale will continue to decrease to what the variable was initially given for a value.
Funny thing is, when I look at the inspector everything should theoretically be working 100% smoothly. In the inspector the lightAmount float value is always working properly and stops where it should all the time, but the scale is not always relative to that number.
I am using the most recent version of Unity 5, here is my code:
public class LightSize : MonoBehaviour {
private GameObject Light;
public float lightAmount;
public float minLight = 0.45f;
public float maxLight = 1.5f;
void Awake()
{
Light = GameObject.FindGameObjectWithTag("Light");
lightAmount = maxLight;
}
void FixedUpdate()
{
if (lightAmount >= maxLight)
{
lightAmount = maxLight;
}
if (lightAmount >= minLight)
{
lightAmount -= 0.001f;
}
else
{
Debug.Log("YOU ARE AT MINIMUM LIGHT");
}
Light.transform.localScale = new Vector2(lightAmount, lightAmount);
}
}
Answer by unity_J016ZU_VZQzYAg · Feb 20, 2019 at 05:05 PM
If I understood you correctly, the variabe is always set to the start you set it to at the beginning ?
Thats unitys editor way of handling stuff, because they are public variables and therefore editable in the editor.
To solve this set the variables private
and if you want to change them in the script, change them, if you want to change them from another script call public methods like setLightMinMax
or getLightMinMax
etc.
@unity_J016ZU_VZQzYAg As I program further I am having a different issue. I have a public method I'm calling from an attack and I checked the value of the light amount. The lightAmount's value is 100% correct and is always working properly, but the scale will not update properly. The only way I can get the scale of the object to change is if something is fired within the light script. For example: If I fire the addLight method within "void FixedUpdate" from a key press it works fine, but if the function is fired from an outside script the number for lightAmount will change, but the scale will not.
New code for the light script is below:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class LightSize : $$anonymous$$onoBehaviour {
private GameObject Light;
private float lightAmount;
private float $$anonymous$$Light = 0.45f;
private float maxLight = 1.5f;
bool AtCheckpoint = false;
void Awake()
{
Light = GameObject.FindGameObjectWithTag("Light");
lightAmount = maxLight;
}
void FixedUpdate()
{
if (lightAmount >= maxLight)
{
lightAmount = maxLight;
}
if (AtCheckpoint)
{
lightAmount = maxLight;
}
if (lightAmount >= $$anonymous$$Light && !AtCheckpoint)
{
lightAmount -= 0.001f;
}
Light.transform.localScale = new Vector2(lightAmount, lightAmount);
}
public void AddLight()
{
lightAmount += 0.3f;
Debug.Log("Add the light");
}
}
This is the Enemy code where I am calling the method to add light:
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class Enemy : $$anonymous$$onoBehaviour {
//Health of enemy and damage it takes
public int health = 1;
public int dmg = 1;
private LightSize lightRef;
void Awake()
{
lightRef = GameObject.FindGameObjectWithTag("Player").GetComponent<LightSize>();
}
void Update()
{
//killing enemy
if (health <= 0)
{
Destroy(gameObject);
}
}
//Damaging Self (enemy)
//This is where I am firing the addLight method
public void DamageEnemy()
{
health -= dmg;
lightRef.AddLight();
}
}