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 /
This question was closed Jan 26, 2018 at 03:37 AM by sunrisepulp for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by sunrisepulp · Jan 26, 2018 at 03:01 AM · listaddindexinsert

Insert object to the list in the script in real-time.

Hello there, my problem is as follow:

I have a normal list. public List targets; I want two objects in this list, and then need to be able to update what object is in the index. So if i have two objects in the index like: Index 1: Object 1. Index 2: Object 2. Then i need to be able to update what object is in object 1 on the fly in the script, is this possible?

I tried using targets.insert(1, object3.transform), but it will keep adding objects to the list, not inserting it into just the index space.

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

  • Sort: 
avatar image
0
Best Answer

Answer by sunrisepulp · Jan 26, 2018 at 03:29 AM

Okay so i kinda figured out how not to make it add forever, but the thing is, i cant get it to insert and replace the current object with the object i want in there. Thanks for taking a look at it, @F14M3THR0W3R

     // Use this for initialization
     void Start () {    
         targets.Add(puck.transform);
         targets.Add(testobject.transform);
     }
  void FixedUpdate()
     {
         //Null.
         if (targets.Count == 0)
             return;
 
         if (Input.GetKeyDown("space"))
         {
            //Trying to replace the object in the first index in the list with this object here.
             if (!targets.Contains(replacementObj.transform))
             targets.Insert(0, replacementObj.transform);
         }
  
     }



Comment
Add comment · Show 5 · 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 Lethael · Jan 26, 2018 at 03:33 AM 0
Share

@sunrisepulp You could try using List.Remove() before actually inserting the object.

 // Not tested
 if (Input.Get$$anonymous$$eyDown("space")) {
     if (!targets.Contains(replacementObj.transform)) {
         targets.Remove (0);
         targets.Insert (0, replacementObj.transform);
     }
 }
avatar image sunrisepulp Lethael · Jan 26, 2018 at 03:37 AM 0
Share

That worked! Thanks very much, feel so stupid now :)

avatar image Lethael sunrisepulp · Jan 26, 2018 at 03:37 AM 0
Share

No worries mate ;)

avatar image Bunny83 Lethael · Jan 26, 2018 at 04:09 AM 0
Share

Sorry but this makes no sense. "Remove" takes a parameter of the element type. $$anonymous$$aybe you meant to use RemoveAt which takes an index. However this also doesn't make much sense. If you want to replace an element in a list at a certain index, just replace it like this:

 targets[0] = replacementObj.transform;

A list can be used exactly like an array. Of course you can only access (read / write) elements at indices which already exist.

avatar image Lethael Bunny83 · Jan 26, 2018 at 04:16 AM 0
Share

@Bunny83 Sorry lol xD I haven't used list's much recently. What i meant to write was:

 if (Input.Get$$anonymous$$eyDown("space")) {
      if (!targets.Contains(replacementObj.transform)) {
          targets.Remove (targets.Item(0));
          targets.Insert (0, replacementObj.transform);
      }
  }

Excuse my ignorance, your solution would definitely work better, I didn't realise you could assign to List's like that, I need to stop using arrays xD

Follow this Question

Answers Answers and Comments

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

Adding a unique element from one list to another list 2 Answers

[ANSWERED] IndexOf and LastIndexOf both returning -1 when an item is on the list. 1 Answer

find out list index of collided object ? 1 Answer

Getting the Index number of lowest 7 ints in list 3 Answers

Adding to list, how to fix NullReferenceException 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