Has anyone notice this or I am just totally new?
In unity CSharp, I was trying to create an inventory system. So I deleted "void Start()" at first cause this usually confused me. then my "idatabase" won't work without Start().
Does anyone konw why? Here is my code:
public class ItemData : MonoBehaviour {
public List<Item> Idatabase = new List<Item>();
Item fish = new Item (001, "Fish", "food");
Item water = new Item(002,"Water","water");
Idatabase.Add (fish);
Idatabase.Add (water);
Debug.Log (Idatabase[0].Name);
Debug.Log (Idatabase [1].Name);
}
public class Item {
public int ID;
public string Name;
public string Type;
public Item(int newID,string newName,string newType){
ID = newID;
Name = newName;
Type = newType;
}
}
in order for your code to run, to need to call it. by enclosing it in a function like Start()
, unity will execute it. it's probably a good idea for you to go through some of the beginner tutorials until you get the hang of things.
@gjf @3dmihai @ScaniX Thank you guys for answering this. It totally makes sense. I did notice that in Unity I have to put every single thing into a function to run. So void Start() is just a function that when game runs then it will start. $$anonymous$$y knowledge is limited cause I can only get to learn through Youtube's videos. It would be nice to have a book or something that I can go through the whole basic stuffs.
Answer by 3dmihai · Aug 25, 2016 at 02:51 PM
You need to enclose your code into a method and then call it (except for the definitions). If you include it in Start(), Unity will automatically call everything inside at game start. But you can write your own (public) method, then instantiate the class somewhere else and call the methods.
Answer by ScaniX · Aug 25, 2016 at 02:59 PM
A class contains property declarations and methods. The declarations and the constructor will be called whenever it is instantiated, which is done by Unity when you add it to a component.
Every other method of it has to be explicitly called. Unity has a huge list of predefined method signatures that you can implement to be invoked by Unity.
Some examples:
void Awake() // after the constructor this is the first one to be called when the scene is initialized. access to your own components is possible
void Start() // called in the second round of scene initialization. access to components of other gamesobjects is possible
void Update() // called once per frame
and like... one million On...() methods to get notified of events like collisions, mouse interaction, etc.
As those methods are called through some reflection mechanism (Messages), you are not really implementing an interface for them.
Your answer
Follow this Question
Related Questions
How can I shift index numbers in a decremental manner? 0 Answers
Inventory Add Item error 2 Answers
What should I use to store item stats? List, Arrays or something else? 0 Answers
Get object to check list for number of specific items 0 Answers
Cant figure out how to implement a inventory system 1 Answer