How do I assign values to variables in the Update function only ONCE?
Hello there!
I'm making a game that has a weapon customization system. This way a player can alter their weapon's statistics to their liking with different attachments. Every attachment is a game object (prefab) that has their own statistics, that are connected to the weapon's script using interfaces. Here is what I've got so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class BRN180 : MonoBehaviour
{
public string Type = "Brownells BRN-180 16 inch";
public float Weight = 2.0f;
public float Cal = 5.56f;
public float baseDamage = 20.0f;
public float MOE = 1.5f;
public float reloadTime = 3.2f;
public GameObject opticUsed;
public GameObject opticObject;
Optic opticStats;
public GameObject magazineUsed;
public GameObject magazineObject;
Magazine magazineStats;
public GameObject stockUsed;
public GameObject stockObject;
Stock stockStats;
private SpriteRenderer partRenderer;
private SpriteRenderer objectRenderer;
void Start()
{
}
void OnGUI(){
GUI.Label (new Rect (200, 100, 300, 60), "Weapon's name: "+Type);
GUI.Label (new Rect (200, 150, 300, 60), "Weapon's weight: "+(Weight+opticStats.Weight+magazineStats.Weight+stockStats.Weight)+" kilogramms");
GUI.Label (new Rect (200, 200, 300, 60), "Weapon's damage: "+(baseDamage*Cal));
GUI.Label (new Rect (200, 250, 300, 60), "Weapon's accuracy: "+(MOE-opticStats.Accuracy*stockStats.Stability)+" MOE");
GUI.Label (new Rect (200, 300, 300, 60), "Weapon's capacity: "+(magazineStats.Capacity)+" rounds");
}
// Update is called once per frame
void Update()
{
opticStats = opticUsed.GetComponent<Optic>();
partRenderer = opticUsed.GetComponent<SpriteRenderer>();
objectRenderer = opticObject.GetComponent<SpriteRenderer>();
objectRenderer.sprite = partRenderer.sprite;
magazineStats = magazineUsed.GetComponent<Magazine>();
partRenderer = magazineUsed.GetComponent<SpriteRenderer>();
objectRenderer = magazineObject.GetComponent<SpriteRenderer>();
objectRenderer.sprite = partRenderer.sprite;
stockStats = stockUsed.GetComponent<Stock>();
partRenderer = stockUsed.GetComponent<SpriteRenderer>();
objectRenderer = stockObject.GetComponent<SpriteRenderer>();
objectRenderer.sprite = partRenderer.sprite;
}
}
This displays the current statistics of the weapon, but the statistics don't actually change... If I try calculating them as I do when I write them on a Label, it keeps getting recalculated each frame.
So, how do I go about changing stats, only when an attachment changes?
Thank you in advance!
Answer by Paolofix21 · Mar 11 at 05:15 AM
You actually don't.
When getting components to store them inside variables the best approach is to do it inside the Start() or even better the Awake().
The Awake() gets called when the object is first loaded or instantiated. The Start gets called after the Awake once, when the object is activated the first time, but before the Update.
Anyway, if you ever for some particular reason need to avoid setting a variable that's already been set, you can use various methoss:
if(!ReferenceEquals(null, variableName))
variableName =...;
or
variableName ??=...;
or (but this is not good for performance)
if(variableName != null)
variableName =...;
I hope this can help you :)
Thank you! I was kinda late to reply, but thank you for your response.
I eventually found my own solution around the problem, but I also tried this for good measures, and it'd work lol