So, I was reading a little bit about constructors and though I have a little experience coding and stuff I still need someone to explain something to me.
First off what is the difference between the next sets of code, the first one is using a constructor and the second one isn’t.
using UnityEngine;
using System.Collections;
public class backShot : MonoBehaviour
{
public class backshot
{
public float BASETIME;
public float FRONTBASETIME;
public float COOLDOWN;
public float FRONTCOOLDOWN;
public float TIMERESET;
public float ZEROTIME;
public backshot(float baseTime, float frontBaseTime, float coolDown, float frontCoolDown,float timeReset, float zeroTime)
{
BASETIME = baseTime;
FRONTBASETIME = frontBaseTime;
COOLDOWN = coolDown;
FRONTCOOLDOWN = frontCoolDown;
TIMERESET = timeReset;
ZEROTIME = zeroTime;
}
}
public backshot backShooting = new backshot(4.0f, 4.0f, 0.4f, 0.1f, 4.0f, 0.0f);
Here is the second one
using UnityEngine;
using System.Collections;
public class backShot : MonoBehaviour
{
public float BASETIME = 10.0f;
public float FRONTBASETIME = 10.0f;
public float COOLDOWN = 10.0f;
public float FRONTCOOLDOWN = 20.0f;
public float TIMERESET = 20.0f;
public float ZEROTIME 0.0f;
void start()
{
}
}
Again, I just want to know the difference of having a class with a constructor and one without a constructor. I also would like to know if it is ok to use constructors in Unity, I have heard it is not a good practice. Thanks in advance for your answers.
Can’t say that I would hear it often that someone considers Constructors bad practice, you just have to make sure that there is actually any need to have them. The only difference between the two is that the one with the constructor would let you define the values of all those variables when you create the object while the other sets the values automatically.
In this case
backShot bA = new backShot();//will use the values that you already set
would do the same as
backShot bB = new backshot(10.0f, 10.0f, 10.0f, 20.0f, 20.0f, 0.0f);
//will use the values that you specify inside the (...)
because the variable values would be the same. So unless you really need to specify any of those values when you create the object w/o using a custom constructor. Also you can add some code to the constructors that prevents the user of the class from entering invalid values. (like , if BASETIME can’t have a negative value, etc…)
The reason using constructors with MonoBehaviours is not recommended is because the Editor could call the constructor at any moment; we have no control over when the constructor of a MonoBehaviour is called. This only applies to MonoBehaviours.
If you have your own class, like you do in your first example, you can use a constructor like you would normally: to create that object. Creation might include allocating memory, setting values or references, or anything else that needs to be done at the time of creation for that object.
As to the difference between those two snippets of code, backshot a nested class, and backShot isn’t. Also backShot is a MonoBehaviour, while backshot isn’t. Which one should you use? That is up to your project and its needs; if a Backshot is an object on its own, but shouldn’t be a MonoBehaviour then make it as a normal class. If Backshot needs to be a MonoBehaviour, then make it a MonoBehaviour.