Setting value in inspector does not update script.
When I alter the value on the inspector before pressing play, the value is not updated to the script, or it keeps the starting value when you reset the inspector. In the code below, I set the inspector value of "WeaponID" to 4, yet it keeps showing up as 0 when I print out the "WeaponID" value. I even tried resetting the component, but it still doesn't work. Am I missing something here?
public class Weapon : MonoBehaviour
{
public int WeaponID;
// Use this for initialization
void Start()
{
Debug.Log("\nID: " + WeaponID);
}
}
It is a pickle. Try increasing the resolution of information you are getting. Here is a version of Weapon that only logs on your say-so.
public class Weapon : $$anonymous$$onoBehaviour
{
public bool WriteLogEntries;
public int WeaponID;
void Start()
{
if (WriteLogEntries)
Debug.Log("ID: " + WeaponID);
}
}
On the instance you want to inspect and modify (and only on that instance), set Write Log Entries to true and Weapon ID to 4. Do that before you start your scene. It should write ID 4 when you start your scene.
If it doesn't, try debugging. I hear that you can use $$anonymous$$onoDevelop to do that. That's especially good if you've done something horrible in a past life for which you wish to atone or if you are planning on doing something horrible in the future for which you need to rack up some preemptive atonement. If you aren't a big fan of pain, get Visual Studio Community edition and set it as your editor, then use it to debug this behavior.
I found out the issue, it was the fact that object this script was originally attached too was a prefab. Thus, it would not be modified. Removing the script from the prefab and breaking the prefab instance by adding a script to the actual object fixed it. Thanks for the suggestion though.
Glad to hear it. Be a good citizen: Document your discovery as an answer and then accept that answer so that someone with a similar problem will be able to easily identify that as one of the possible causes.
If you feel weird accepting your own answer, get someone to shill for you. ;)
Your answer
Follow this Question
Related Questions
Unity 4.1.5 is not showing certain public variables in the inspector 1 Answer
GameObject fields evaluate to null, but they are actually assigned and working 1 Answer
Public fields not showing in inspector 1 Answer
Destroy gameobjects several times in a scene. Also destroy other instances of that gameobject. 2 Answers
How to add a editing option to variables in a script in Inspector? 0 Answers