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 /
avatar image
0
Question by Harryliu · May 19, 2016 at 05:17 AM · listcomponentclass

Adding to a List and putting it through a function?

I watched the tutorials regarding Lists and Dictionaries but it was a bit confusing to me as I have never worked with classes before. I am sure the Unity Community is willing to help me out here.

I have set up two scripts, one for constructors and some functions, one for adding items to the list. My idea here is the script that adds to the list is like sending an ingredient to the pot. And the script that puts the items in the list through a function is like cooking the ingredients inside the pot.

Like:

 public var ingredients = new List.<ingredientsToCook>();
 
 function OnTriggerEnter (Col : Collider) {
 
     if(Col. CompareTag("Pot"))
     {
         ingredients.Add("Potatoe", 60)
     }
 }

This will be in a public class called something like Ingredients.

And inside the pot:

 public class items {
 
     var itemName : String;
     var cookTime : int;
 
     public function items(newName : String, newTime : int){
 
         itemName = newName;
         cookTime = newTime;
     }
 
     for(var ingredient in ingredients)
     {
          //cooks the ingredients
     }
 
 }


Does this concept work? But what if each ingredient has a different time to cook? how do I access each item's different var timeToCook : float = x inside a list and cooks the food accordingly? I will look at some youtube videos in the meantime.

thanks so much.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by techmage · May 19, 2016 at 06:15 AM

I think this is probably more what your looking to do:

  public class Pot : MonoBehaviour {
     List<Ingredient> ingredients = new List<Ingredient>();
      
     void OnTriggerEnter(Collider collider) {
         if (collider.CompareTag("Pot")) {
             ingredients.Add(new Ingredient("potato", 60));
         }
     }
 
     public void Cook() {
         for (int i = 0; i < ingredients.Count; ++i) {
             //whatevs
         }
     }
 }
 
 public class Ingredient {
     string _mame;
     string _time;
  
     public Ingredient(string name, int time) {
         _mame = name;
         _time = time;
     }
 }
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 jdean300 · May 19, 2016 at 05:56 AM

It seems like you are using JavaScript, which is not my string suit, but hopefully I'll get most of it right

Your for loop is not going to work right there. You want to put that inside of a function - probably on Update(). It would look something like this:

 function Update()
 {
     for (var ingredient in ingredients) {
 
         //Have a variable on each ingredient that says whether it has been cooked yet. If it has been cooked, continue to the next item
         if (ingredient.Cooked)
             continue;
 
         //Have a variable on the ingredients to track how long they have been cooking
         ingredient.TimeSpentCooking += Time.deltaTime;
 
         if (ingredient.TimeSpentCooking >= ingredient.TimeToCook){
             ingredient.Cooked = true;
         }
     }
 }

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

44 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 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 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

A node in a childnode? 1 Answer

Best way to get variable from another script 1 Answer

Array of Objects of a Class 1 Answer

How to Copy a Component if it contains List? 1 Answer

how do I remove a class item from a list? 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