Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by RuilinBu · Aug 25, 2016 at 11:41 AM · liststartinventory system

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;
 }
 
 }



Comment
Add comment · Show 2
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 gjf · Aug 25, 2016 at 12:34 AM 1
Share

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.

avatar image RuilinBu · Aug 25, 2016 at 03:45 PM 0
Share

@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.

2 Replies

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

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.

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
avatar image
0

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.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


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