Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
2
Question by ZG_UCSC · Oct 07, 2016 at 06:25 PM · scripting probleminspectorpublic variable

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZG_UCSC · Oct 07, 2016 at 11:41 PM 0
Share

Putting it in Awake() did not work. Please see my commend to @txzeenath.

avatar image
-1

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).

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZG_UCSC · Oct 07, 2016 at 11:40 PM 0
Share

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?

avatar image
0

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);
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZG_UCSC · Oct 07, 2016 at 11:41 PM 0
Share

Please see my commend to @txzeenath.

avatar image
0

Answer by MagyarPeter · Apr 16 at 02:41 PM

[System.NonSerialized] public float value1 = 123;

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Some Public Variables doesn't display in Inspector 2 Answers

Adding in other variables through the inspector in a public string 1 Answer

Custom Editor - Show variable if bool is true in class list 1 Answer

Passing Script as a Public Variable,Passing Script as Public Variable 2 Answers

When would I encapsulate variables? 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges