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 d112570 · Sep 30, 2013 at 07:49 PM · valuessetelementcreating

When creating new elements how do you auto set values?

When you create a new elements, how do you set values automatically via script? I am planing of adding values to each Rotational Speed. If I find out how to for Rotation Speed I can also have set values for the rest.

alt text

Here is the script.

 [System.Serializable]
 public class rotationalSpeed {
 [Range (-100, 100)]
 public float Surface;
 [Range (-100, 100)]
 public float Cloud1;
 [Range (-100, 100)]
 public float Cloud2;
 [Range (-100, 100)]
 public float Cloud3;
 [Range (-100, 100)]
 public float Ring1;
 [Range (-100, 100)]
 public float Ring2;
 [Range (-100, 100)]
 public float Ring3;
 }

 public rotationalSpeed rotationalSpeed;

bildschirmfoto 2013-09-30 um 21.40.39.png (20.7 kB)
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

1 Reply

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

Answer by Jamora · Sep 30, 2013 at 10:10 PM

Because your class does not inherit from MonoBehaviour, you can use the default constructor to initialize its values. The constructor is always called when creating a new instance of that class.

Your class would look something like this

 [System.Serializable]
 public class rotationalSpeed {
 
 //all the variables, clouds, rings, etc. are listed here
 
 //This is the constructor.
 //It must have the same name as your class.
 public rotationalSpeed(){
     Surface = 1.0f;
     Cloud1 = 2.6f;
     ///etc.
 }
 }
Comment
Add comment · Show 8 · 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 d112570 · Sep 30, 2013 at 10:43 PM 0
Share

I tried exactly as written but, I got this error view below- Here's the code.

 [System.Serializable]
     public class rotationalSpeed {
         [Range (-100, 100)]
         public float Surface;
         [Range (-100, 100)]
         public float Cloud1;
         [Range (-100, 100)]
         public float Cloud2;
         [Range (-100, 100)]
         public float Cloud3;
         [Range (-100, 100)]
         public float Ring1;
         [Range (-100, 100)]
         public float Ring2;
         [Range (-100, 100)]
         public float Ring3;
         }
     
     public rotationalSpeed() {  // Error:  Class, struct, or interface method must have a return type.  
             Surface = 1.0f;
             Cloud1 = 1.5f;
             Cloud2 = 1.2f;
             Cloud3 = 0.5f;
             Ring1 = 1.0f;
             Ring2 = 0.6f;
             Ring3 = 0.3f;
         }

Where could the error be?

I also tried which does nothing

     [Range (-100, 100)]
      public float Surface = 1.0f;

I tired this which also gives error

     public rotationalSpeed rotationalSpeed(){
avatar image TheRaven · Sep 30, 2013 at 11:53 PM 0
Share

Your constructor is outside the class. Put it inside the class and then all should be well.

ie. $$anonymous$$ove the "}" form line 17 above and put it at line 28

avatar image d112570 · Oct 01, 2013 at 09:23 AM 0
Share

No more errors, but the results are still all 0,0,0,0,0,0,0. Here is what I have so far. When I create new elements I would like it to have factory settings (Standard). So only thing you have to work on is the name, and everything else is already set. Unless you want it different for some reason.

 [System.Serializable]
     public class rotationalSpeed {
         [Range (-100, 100)]
         public float Surface;
         [Range (-100, 100)]
         public float Cloud1;
         [Range (-100, 100)]
         public float Cloud2;
         [Range (-100, 100)]
         public float Cloud3;
         [Range (-100, 100)]
         public float Ring1;
         [Range (-100, 100)]
         public float Ring2;
         [Range (-100, 100)]
         public float Ring3;
         public rotationalSpeed() {
             Surface = 1.0f;
             Cloud1  = 1.5f;
             Cloud2  = 1.2f;
             Cloud3  = 0.5f;
             Ring1   = 1.0f;
             Ring2   = 0.6f;
             Ring3   = 0.3f;
         }
     }
 
     [System.Serializable]
     public class StarSystem
         {
         public string name;
         public Category Category;
         public Class Class;
         [Range (-180, 180)]
         public float axisTilt = 0;
         public bool axisLeansToStar = false;
         public bool hasAsteroidField;
         public rotationalSpeed rotationalSpeed; // Here is my rotational speed
         public Lightning Lightning;
         public cameraLocation cameraLocation;
         public GUISetup OnScreen;
         }
     
     public StarSystem[] SystemSetup;
avatar image Jamora · Oct 01, 2013 at 11:27 AM 0
Share

It is very bad coding practice to name your variables exactly the same as its type. In fact, I'm a bit surprised it doesn't throw a compiler error. In C# it is customary to capitalize types, and have the variables lower case. So consider capitalizing your class names, e.g. public class RotationalSpeed.

You also need to create a new instance of your class RotationalSpeed in StarSystem. So line 38 would be

  public RotationalSpeed rotationalSpeed = new RotationalSpeed();


Unity apparently tries to do this but doesn't quite seem to make it work...

avatar image d112570 · Oct 01, 2013 at 11:39 AM 0
Share

So does it means its not possible to set the variables? The reason I make both the same name/size is because I get an error. So I keep both same size and name, I tried the other way but then rotationSpeed does not work and gives an error. Anyways, I still get no value in my inspector creating a fresh new element, still all 0,0,0,0,0,0,0.

Edit: public rotationalSpeed() { // Also needs to be Capitalized for the coding practice to work. Still all have 0 values.

Show more comments

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

16 People are following this question.

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

Related Questions

Adding elements to a list is changing the initial values. Need help! 0 Answers

Get Value/String From GUIText 3 Answers

Vector Set() method VS new 0 Answers

Unexpected values at the inspector when initializing member values from built-in arrays 0 Answers

How will i update my table values every time? 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