- Home /
How to prevent the value in the Inspector from overriding the value in a script?
I am trying to create supernova explosion in my game. The damage and effective range of the explosion is determined at runtime by its mass defined in a public variable defined in a script. I have created two game objects for this kind of explosion. One for the supernova itself, which has a script containing the mass variable and this variable is assigned with a random value in the Start() method. Another object is the explosion, which also has a script calculating the damage range of the explosion. Both objects have a public GameObject variable referencing each other. My plan is to have the supernova instantiating an explosion object and destroy itself. The instantiated explosion uses the GameObject variable associated with the instantiating supernova to retrieve its mass value using GetComponent in its Start() method. Yet the explosion script could not retrieve the value correctly. The value retrieved is the value set in the Inspector for the supernova script, not the value assigned at runtime.
The script for the supernova:
public GameObject Explosion;
public int Mass;
void Start () {
Mass = Random.Range(10000,20000); //Value assigned at runtime
}
The script for the explosion:
public GameObject Supernova; //Reference to the instantiating supernova game object
public int Mass;
void Start () {
Mass=Supernova.GetComponent<SupernovaControl>().Mass; //Only the value in the inspector is returned
}
I tried to alter the mass value in the Inspector for supernova. Its own mass variable would still be the value assigned in Start() method, but the value retrieved by explosion script would become the value in the Inspector. So I think the value in the Inspector has overridden the runtime value when retrieved from another script. Is it possible to prevent this from happening? I only want the runtime value to be retrieved.
Answer by brazmogu · Oct 07, 2016 at 07:43 PM
As @txzeenath said, it could be the case that you're grabbing the value before Start() has run. Start() runs right before the first update cycle for the object. You should use Awake() if you want something to be done right as the script is instantiated.
Setting the Explosion's Mass in its Awake() method and grabbing it in the Supernova's Start() method should yield the intended result.
Putting it in Awake() did not work. Please see my commend to @txzeenath.
Answer by txzeenath · Oct 07, 2016 at 07:31 PM
You can either not make it public. Or mark it with [System.NonSerialized] which hides it from the inspector.
Also, when linking scripts that that, there's no guarantee that one script is ready before the other, unless you use the script ordering in project settings to force it.
You may be trying to grab "mass" before the Start function has even ran. (depending exactly how it's set up that is).
I tried to retrieve the value in Awake() method in the explosion script. Not working. But the following would work in the supernova script:
var thisExplosion = Instantiate(Explosion, gameObject.transform.position, Quaternion.identity) as GameObject;
thisExplosion.GetComponent<ExplosionControl2>().$$anonymous$$ass = $$anonymous$$ass;
Destroy(gameObject);
Why this one worked?
Answer by elenzil · Oct 07, 2016 at 08:05 PM
i think both brazmogu & txzeenath are correct when they guess that you're grabbing the value before Start() has executed.
for what it's worth, another way to hide things from the inspector is to wrap them in setter/getters.
eg
class SomeClass : MonoBehavior {
private float theVal;
public float TheVal {
get {
return theVal;
}
}
}
someOtherCode() {
SomeClass foo;
DoSomethingWith(foo.TheVal);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612094203im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Some Public Variables doesn't display in Inspector 2 Answers
is there a way to minimize amount of public items in inspector? 1 Answer
Add a new script line to my script with text from textboxes after button in the Inspector is pressed 0 Answers
Cannot assign Public GameObject variable in Inspector... 2 Answers