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
1
Question by jjesh · Nov 18, 2015 at 04:52 PM · c#transformlistselectionindex

I need help finding the index of an object in a list.

Hi, im trying to have it so that the script finds the current selected object, and then finds the index of that in an already made list. This is what I'm trying:

 Transform currentSelection = Selection.activeTransform;
 int currentSelectionIndex = myList.IndexOf (currentSelection);


The hope is that I would be able to affect the currently selected object with myList[currentSelectionIndex], but I can't get it to work.

Edit: The problem I'm getting is that everything I select (that's in the list) is saying the index is -1.

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 beppim · Nov 18, 2015 at 04:58 PM 0
Share

Did you check if Selection.activeTransform really returns something !null ? Just to check :)

avatar image jjesh beppim · Nov 18, 2015 at 05:03 PM 0
Share

Wow, didn't even think to check for that! $$anonymous$$y problem is happening before that, though. It says "error CS0103: The name `Selection' does not exist in the current context"

Edit: Realized I was missing using UnityEngine, just added that in. The problem I'm having now is that the currentSelectionIndex is returning as -1 for everything I select

1 Reply

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

Answer by SeasiaInfotechind · Nov 20, 2015 at 06:09 AM

HI jjesh,

List returns index to be -1 if it is not able to find that object in the given list. So your current selection object is not returning the data that is contained in list. It could be naming issue or anything. So please check that.

A sample script to show you that

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class abc : MonoBehaviour {
     List<int> iList = new List<int>();
 
     // Use this for initialization
     void Start () {
         iList.Add(2);
         iList.Add(3);
         iList.Add(5);
         iList.Add(7);
     }
 
     // Update is called once per frame
     public void ButtonClicked ()
     {
         int currentSelectionIndex = iList.IndexOf(7);
         print (currentSelectionIndex);// It will return index 3
         currentSelectionIndex = iList.IndexOf(9);
         print (currentSelectionIndex);// it will give -1 as 9 is not present in the list.
     }
 }
 


I hope you find out whats getting wrong.

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 jjesh · Nov 20, 2015 at 04:34 PM 0
Share

Thank you for your help! What I'm trying to do is find the gameobject which contains the input field I'm typing in, but it turns out how I was doing it was finding the gameobject i gad selected in the editor. Any idea how I would do this?

avatar image SeasiaInfotechind jjesh · Dec 07, 2015 at 07:20 AM 0
Share

Hi, Your query is not very clear . Elaborate it

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

34 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

Related Questions

List update 0 Answers

Unknown Argument Out of Range Index Error On Card Game 1 Answer

Character Selection 0 Answers

How to output a List of Strings to canvas text? 0 Answers

How do I make something move for only a few seconds C# 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