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 O8PC · Feb 22, 2017 at 08:32 AM · c#gameobjectlist

How to select the Nth gameObject in a list and put it into a gameObject variable

I'm trying to put a check in my game where it disables everything that doesn't have the same ID as the index. This is the system I have set up:

 void WeaponSelect() {
     selector = Input.GetAxis ("Scroll");
     index += selector;
     if (index > weapons) {
         index = 1;
     }if (index < 1) {
         index = weapons;
     }
     check += 1;
     checkedObject = weaponList[check];
     if (checkedObject.GetComponent<WeaponID> ().ID == index) {
         checkedObject.GetComponent<WeaponID> ().onabled = true;
     } else {
         checkedObject.GetComponent<WeaponID> ().onabled = false;
     }
     if (check > weapons) {
         check = 1;
     }
 }

but this gives off this error:

 error CS1502: The best overloaded method match for `System.Collections.Generic.List<UnityEngine.GameObject>.this[int]' has some invalid arguments

and

  error CS1503: Argument `#1' cannot convert `float' expression to type `int'

If this isn't the right way to do this, what is? Can anybody please help?

Comment
Add comment · Show 1
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 Deathdefy · Feb 22, 2017 at 06:41 PM 0
Share

Can you include the variable declarations so we can see the types you have?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by gameplay4all · Feb 22, 2017 at 07:18 PM

You want something like this:

 List<GameObject> weaponList = new List<GameObject>();
 float scrollTracker;
 float scrollSensitivity = 3.0f;
 int weaponIndex;
 GameObject selectedWeapon = 0;
 
 void Update(){
 scrollTracker += Input.GetAxis("Scroll") * scrollSensitivity * Time.deltaTime;
 scrollTracker = Mathf.Clamp(scrollTracker, 0, weaponList.Count - 1); 
 weaponIndex = (int)Mathf.Round(scrollTracker);
 selectedWeapon = weaponList[weaponIndex];
 }

Lists use indices, which are ints. But GetAxis returns a float, so let's say you have the index 2 and GetAxis returns 0.6f. Then the resulting index would be 2.6f, which is still 2 when it is converted to an int. So that why you need to use a float value to keep track of the scrolling. Hope this helps, please take a look at my example code and try to understand it :)

Good luck! -Gameplay4all

Comment
Add comment · Show 2 · 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 Deathdefy · Feb 22, 2017 at 07:29 PM 0
Share

Based on the errors you are getting, Im assu$$anonymous$$g your variable checkis a float. If that is true you could be attempting to get an index from your list of like 1.56f which is not an index you can grab. Try casting it as a int and it should fix your problem.

  checkedObject = weaponList[(int)check];

If this isn't the issue then you'll have to provide the entire script so I can look at it :).

avatar image O8PC · Feb 23, 2017 at 08:48 PM 0
Share

How would I disable everything that Isnt selectedWeapon?

Thanks for the help :D

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

9 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Get first gameobject in a list and cycle through on keypress 1 Answer

How to add elements from a List of another class to another List (C#) 1 Answer

Distribute terrain in zones 3 Answers

Ordering a list of GameObjects 3 Answers

Runtime instantiation based on XML 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