- Home /
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.
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;
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.
}
}
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(){
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
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;
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...
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.