Question About Defining and Calling Static Variables
Hello,
I am a very new Unity user and I am trying to make a game that will include character customization. I would like to ask players to define their character's height. I am not at all sure how to do this. My strategy was going to be to have one script that is basically a list of all variables that will define and track them (this script will be known as VariableList), and then another script that will allow me to change the height value when the player clicks a button to choose their height (this one is known as VariableChanger). The Height variable is defined like so, in VariableList:
using UnityEngine;
using System.Collections;
public class VariableList : MonoBehaviour
{
static public string Height;
// Use this for initialization
void Start()
{
Debug.Log("My height is " + Height);
}
}
It also features a little debug function that I have been trying to use. Then I would like to call this variable in another script, VariableChanger, which I will plug in to my buttons. This is where things start to go wrong. I have absolutely no idea how to write such a script. Here is my attempt so far:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class VariableChanger : MonoBehaviour
{
VariableList.Height = VariableList.Height
public string HeightUpdater(string VariableList.Height)
{
VariableList.Height = "56";
Debug.Log("My height is now " + VariableList.Height);
return VariableList.Height
}
}
VariableList.Height = VariableList.Height was my effort to resolve an error where the script didn't seem to be able to fetch the static variable Height from the other script, but it didn't help. I sort of doubt that this is the right approach in general, but this code in particular is full of errors. Even when I tweak it to try and remove the errors and allow the game to run, no option appears when I plug it into the On Click() property of the button to update VariableList.Height or have it do that on click or anything like that. I am very confused, and help would be appreciated!
Answer by tormentoarmagedoom · May 15, 2019 at 11:02 AM
First,
Why this line in VariableChanger ? what you think it does?
VariableList.Height = VariableList.Height
Sincerly, you are trying to build a car without learning before to build a bike...
Before commencing create functional scripts, learn:
private variables public variables
learn how must be called, readed or writed
then learn what is a static variable and how they work
And after that, try to do some simple and basic scripts using what you learnt.
Reallym, spend one week only learning, reading, watching tutorials about the same again and again, ultin you are sure you know how they work.
If you try to build a house but doesn know about materials, the house will fall for sure.
Good luck!
I am trying to learn all of this by doing. I've found that with a lot of things if I get bogged down in theoretical stuff I never actually learn how to use the skill I am supposed to be developing. What I would like is specific advice about how one would go about updating a static variable that's defined in one script using a function in another script.
Your answer
![](https://koobas.hobune.stream/wayback/20220612201412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Change of static variable 1 Answer
Scene stores variables after scene reload 0 Answers
Game Objects not using same script independently. 1 Answer
Static Class Strings not truly null? 1 Answer