- Home /
Variables not setting correctly
Hello,
I've got a few variables
public int health;
private int test = 10;
Changing it in the Start function
public void Start()
{
test = health;
Debug.Log("test: " + test);
}
Which displays: 1, as intended. But later when I try to log the variable again, I get an incorrect number.
public void Touch()
{
Debug.Log("test: " + test);
}
Displays: 10.
If I change this variable to 20. The Touch function will still display 10, until I re-open Unity.
What is happening...?
Are you sure this is your exact code? The problem you describe would happen if on 3 in your start section you had this ins$$anonymous$$d:
int test = health;
public int health = 100;
private int test = 10;
void Start() {
//changing test value to health
test = health
//print log
print("Health is about " + test.ToString());
//Output should be (Health is about 100)
//whenever you print it its value same as health
}
If the variable is public, it will allways get the int you setted in the inspector.
The full class:
public class $$anonymous$$oleObject: $$anonymous$$onoBehaviour
{
public int health;
private int test = 200;
public void Start()
{
test = health;
Debug.Log("Test 2: " + test);
}
public void Touch(int mod)
{
Debug.Log("Test: " + test);
}
}
The variable 'test' was set on the first run, changing the variable later won't change anything.
Here is a screenshot: http://gyazo.com/dc86717e1cd4efa2a2622c924866e6d9.png
Note that changing health
in the inspector won't change test after that Start
was called. integers are passed by value, not by reference
Answer by 0Zidane0 · Jun 20, 2014 at 03:31 PM
I ran into a similar situation just now. It turned out I was only changing my public variable on a single instance of a prefabbed object in the editor, when I wanted to change it on the prefab itself, leading to a lot of confusion. Is this perhaps what you're doing as well?
This was a while ago, sorry for not answering how I fixed it. But my problem was that I was setting the variable of the prefab ins$$anonymous$$d of the create object. This caused unwanted behaviour. Why this behaviour happend I don't know, it could be a bug in Unity.
I'm going to mark this as the answer, so people know what the solution is :)
Answer by TheRichardGamer · Dec 25, 2013 at 07:47 PM
Since your health variable is empty, the test variable doesn't know what to be set to, so you'll need to enter a value in the health variable for it to work.
The health variable is set in the unity editor. But that's not the point... I already set in the post that health and test output the correct number: 1. But after the start function test outputs a completely number.
@Bio Discus, sorry but that last sentence doesn't make any sense. Could you try reformulating your sentence or if you are having trouble translating from a different language write both what you want to say in your mother tongue and google-translate's result?
Sorry for that.
The health variable is set in the unity editor to 1. The test variable is set to the health variable in the Start function, this logs: 1 (test = health). But in the Touch function the test variable behaves weirdly, it outputs a completely different number (See this image: http://gyazo.com/dc86717e1cd4efa2a2622c924866e6d9.png).
I just checked the log inside the Update function, and everything logs as it should be logged. Is it possible there is a problem with the function: GetComponent(); ?
Please show us the code you are using for your GetComponent()
call.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Loading a Constant Array Of Integers 0 Answers
Debug.Log a gameobject in C ? 2 Answers
How do I make my own Remote Settings backend like Unity Analytics Remote Settings? 0 Answers