Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 shatley123 · Jan 03, 2014 at 06:25 AM · functionparametersargumentellipse

How to use unlimited parameters in a function? (JS)

Hi! For my game i'm making a space colony game, were you can build buldings, so i'm making a function that builds the buildings. But I would like to make it so it can have an unlimited amount of integer values put in. These would be the resources required to build the building, and i would check with a for loop if you have enough and if you do you can build the building. But how to I do this? I know in java you would use ... but how do you do this in javascript?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by soft_sound · Jan 03, 2014 at 07:15 AM

This seems vague to me, this would be pretty similar to regular Java or javascript concept wise.

Hmmm... Well, this might give you some ideas.

 function Start ()
 {
     //call function & check if it returns true
     if (checkResources(3, 5, 2, 8) == true)
         Debug.Log("Building");
 }
 
 //setup function...
 function checkResources(wood, woodAmountNeeded, metal, metalAmountNeeded) {
 
     if(wood == woodAmountNeeded && metal == metalAmountNeeded)
     {
         Debug.Log("You have enough wood & Metal");
         return true;
     }
     else
     {
         Debug.Log("not enough supplies");
         return false;
 
     }
 
 
 }

I'm not sure why you need a for loop for this, nor why you would need unlimited integers or what that even means...Can't you just add parameters and arguments as you need them? Also sorry, I normally write in C# so, my unityscript is rather on the sorry side.

Comment
Add comment · Show 1 · 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 T27M · Jan 03, 2014 at 07:26 AM 0
Share

If I understand the question correctly; the only easy way I know is to use an array or list. That said I would go with how you did it.

 #pragma strict
 
 import System.Collections.Generic;
 
 var someNumbers = new List.<int>(); 
 
 function Start () {
 
     for(var cntr = 0; cntr < 50; cntr++)
         someNumbers.Add(cntr);
     
     LoopListFunction(someNumbers);
 }
 
 function LoopListFunction(myList){
     if(typeof(myList) == List.<int>){
         for(var value : int in myList){
             Debug.Log(value);
         }
     } else {
         throw "Type Error"; 
     }
 }

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

20 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

Related Questions

A node in a childnode? 1 Answer

Call Function with Collision-parameter 1 Answer

boolean as function parameter/argument 2 Answers

help with objexporter- passing parameters to C# function from JS 1 Answer

Variables Set by Function Outside of Script 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