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 BHS · Dec 05, 2012 at 08:44 PM · arraysadd

Add GameObjects to an Array List on RayCastHit

How do I do I add to an array? I have been looking through everything I can find and I still can't figure this out. When I add the item to my array it removes the "picked up" object from my array and replaces it with the most currently picked up item. Any help with this would be greatly appreciated I'm going crazy trying to figure this out.

Some screen shots to clarify. I want to add the axe to the array not the old chest. (Sorry they're so dark they were't when I took them. The crosshair is looking at a chest)

alt text alt text

wanttolootaxetoarray.png (392.8 kB)
addedchestinsteadofaxe.png (385.8 kB)
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
Best Answer

Answer by Landern · Dec 05, 2012 at 09:02 PM

EDIT #3:

I thought that lootID1 was a list of gameobject, i've updated to fix it.

END EDIT #3

Okay, lets try this again, i'm going to assume a loot container COULD maybe have more then one item, either way...:

     if (Physics.Raycast(ray, hit, 2.5)) 
     {              
           nameHolder = hit.collider.gameObject.name;

           if (Input.GetButtonDown ("Fire1"))
           {
                if (hit.transform.gameObject.GetComponent(AddToLoot))
                {              
                      var lootContainer:AddToLoot = hit.transform.gameObject.GetComponent.<AddToLoot>(); // Get a reference to the AddToLoot object
                      // storedLootFixedArray should be instantiated in start or when it is declared.
                      if (storedLootFixedArray == null)
                            storedLootFixedArray = new List.<GameObject>();

                      storedLootFixedArray.Add(lootContainer.lootID1); // add references to items to new list 
                      lootContainer.lootTaken = true;
                }
           }
     }
     else
     nameHolder = "";
Comment
Add comment · Show 8 · 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 BHS · Dec 05, 2012 at 09:26 PM 0
Share

I should have explained better.

The lootID1 is from the script AddToLoot. lootID1 holds a gameobject. When the raycast hits a gameobject (Example: a barrel) with the script AddToLoot it gets the lootID1 gameobject I've added (which is an axe) and adds it to the array which is like a temporary inventory. Everything works fine but when I add a new item the array resets and only keeps the newest added item ins$$anonymous$$d of adding it to the array.

lootTaken = true; is so that the player can't get the loot more than once. Once lootTaken is true the item is taken and cannot be taken again.

Does this make sense? I will try the changes you made and see if it works thanks.

avatar image BHS · Dec 05, 2012 at 09:35 PM 0
Share

I tested this out and it's adding to the array but not properly.

I need it to add the lootID1 gameobject I added via the other script (example: Battle Axe). What your version is doing is adding the gameobject that's holding the script (example: Barrel, Old Chest, Crate)

The barrel has the script AddToLoot I want to get the lootID1 gameobject not the barrel.

Thanks I appreciate the help.

avatar image Landern · Dec 05, 2012 at 09:37 PM 0
Share

i was assu$$anonymous$$g that lootID1 was not a gameobject givent he field/property name.

avatar image BHS · Dec 05, 2012 at 09:42 PM 0
Share

I added some screenshots to clarify.

avatar image Landern · Dec 05, 2012 at 10:55 PM 0
Share

thanks, updated answer, also assumed it's possible that your lootID1 list of gameobjects may be more then one item.

Show more comments

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

10 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

Related Questions

Problems creating new List of type GameObject and adding GameObject to it. 1 Answer

Filling up an array with GameObjects 0 Answers

Help adding transform to end of array 1 Answer

Loading function into array using GameObject.Find 1 Answer

Getting the size of an array of GameObjects 2 Answers


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