Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by DesiQ · Jul 24, 2012 at 12:57 PM · variableinspectorpublicnon-staticinitialise

Can't initialise public vars in Inspector (C#)

Summary:

  • I'm trying to use the Inspector to set a value on a non-static public var, but the game ignores the Inspector and uses only the scripted values.

Intended behaviour:

  • I have a class named PlayerControls, which calls a class named Shotgun. If I set Shotgun's _ProjectilesPerShot to 10 in the Inspector, the script should instantiate 10 projectiles.

Actual behaviour:

  • The script ignores the _ProjectilesPerShot value I've set in the Inspector (I am setting it on the prefab itself). The asset variables (sound, prefab) work fine, though.

  • Initialising _ProjectilesPerShot in the script makes it work, but the Inspector doesn't change this value at runtime. Setting _ProjectilesPerShot = 1 in the script but = 10 in the inspector only instantiates one projectile.

Things I have tried:

  • I added a Debug.Log in the Shotgun constructor to show the values of projectilesPerShot and _ProjectilesPerShot after assignment. Both values remain unchanged (0 and 0 if uninitialised in the script, n and n if _ProjectilesPerShot = n in the script).


    PlayerControls.cs

    using UnityEngine; using System.Collections;

    public class PlayerControls : MonoBehaviour { public Shotgun shotgun;

        void Awake ()
         {
             shotgun = GetComponent<Shotgun>();
         }
         
         void Update ()
         {
             if (Input.GetMouseButtonDown(0))
             {
                 shotgun.fire();
             }
         }
     }
    
    

    Shotgun.cs

    using UnityEngine; using System.Collections;

    public class Shotgun : MonoBehaviour { // Inspector-accessible variables. public int _ProjectilesPerShot;

        // Inspector-accessible asset variables.
         public GameObject projectile;
         public AudioClip fireSound;
         
         // Internal variables.
         private int projectilesPerShot;
         
         public Shotgun ()
         {
             projectilesPerShot = _ProjectilesPerShot;
         }
         
         public void fire ()
         {
             audio.PlayOneShot (fireSound, 0.05f);
             for (int i = 0; i<projectilesPerShot; i++)
             {
                 Instantiate (projectile, gameObject.transform.position, Quaternion.identity);
             }
         }
     }
    
    
    

Any help would be greatly appreciated!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer Wiki

Answer by termway · Jul 24, 2012 at 01:18 PM

Try to use Start() method instead of class constructor

 public Shotgun ()
  {
         projectilesPerShot = _ProjectilesPerShot;
  }
 =>
  public void Start ()
  {
         projectilesPerShot = _ProjectilesPerShot;
  }

Read section 2 & 7 : http://docs.unity3d.com/Documentation/ScriptReference/index.Writing_Scripts_in_Csharp_26_Boo.html

Hope it solve your problem!

Comment
Add comment · Show 2 · 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 Bunny83 · Jul 24, 2012 at 01:55 PM 1
Share

Exactly. The deserialization happens after the object has been created. Therefore the constructor is already finished when the serialized value is assigned.

That's also the reason why the docs always tell you to not use the constructor. Use Awake() or Start().

avatar image DesiQ · Jul 24, 2012 at 07:02 PM 0
Share

Thank you, this was exactly it! Not sure how I missed that. :/

avatar image
0

Answer by MihaPro · Jul 24, 2012 at 03:30 PM

Don`t use constructor for MonoBehaviour derived classes. Use Awake and Start for initialization.

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

Invisible Public Variable in Inspector 5 Answers

Public "char" parameter not showing on inspector? 2 Answers

How to set a public variable to a gameObject in the inspector through coding 1 Answer

Cannot assign Public GameObject variable in Inspector... 2 Answers

Public variable not showing in Inspector(Solved) 1 Answer


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