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 /
This question was closed Mar 16, 2014 at 06:04 PM by z3nth10n for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by z3nth10n · Jan 11, 2014 at 10:01 PM · errorgameobjectclearbuiltin array

Clearing Dynamically GameObject Array

Hi,

well I created an array for my weapons, and when I change the selected slot of my Inventarory, I want to clear the last gameobject charged by the following code:

 bool slotChanged;
 int selectedSlot; //That changes
 float tempselectedSlot;
 int tempSelected;
 GameObject Manager; //A simple GameObject that contains a Store
 GameObject wCamera; //Another Script that contains PlayerWeapons
 PlayerWeapons pWeapons;

 void Start() {
     pWeapons = wCamera.GetComponent<PlayerWeapons>();
 }

 void Update() {
 
 if (selectedSlot > 9) { //That supposedly avoids the array out of index error, but that doesn't avoid anything... Because sometimes that fails...
     tempselectedSlot = 0;        
 } else if(selectedSlot < 0) {
     tempselectedSlot = 9;                
 }
 
 tempselectedSlot -= Input.GetAxis("Mouse ScrollWheel")/2; //Mouse ScrollWheel is equals to 1.
                     
 selectedSlot = (int)tempselectedSlot;
 
 slotChanged = selectedSlot != tempSelected;
                         
 tempSelected = selectedSlot;

     if(slotChanged) { //If the selected Slot changes, that actives, and it supposedly clears the main weapon GameObject array, and if there is a new item in the selected slot, charges it...

         pWeapons.ClearArray(); //A JS Function that supposedly clears the main array.

         if(Slots.HotBarSlots[selectedSlot] != null) { //If the selected Slot contains a Element...
                    Manager.transform.Find("StoreController").gameObject.GetComponent<DBStoreController>().equipWeapon(pAvoider.transform.Find(Slots.HotBarSlots[selectedSlot].itemname).gameObject.GetComponent<WeaponInfo>(), 0); //That Calls the function equipWeapon, from Manager -> StoreController -> DBStoreController (JS Script)
         }
                             
     }
 
 }

equipWeapon works so I won't put the code...

But ClearArray doesn't works, so there is the problem:

 //The problem is that it's in JS (because it's a very big third party code)
 
 function ClearArray() {
 var Dweapons = new Array(weapons); //Create a dinamically array from a builtin array
 Dweapons.Clear(); //Clear it entire
 Dweapons.length = 1; //Set the default length
 weapons = Dweapons; //And return a builtin array...
 }
 
 //But for some reason it desn't work, the GameObject is still there when there isn't a item on the selected slot...

All is in the code comments, so read it, and tell me what I'm doing wrong...

Thanks in advance... Bye.

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 agies1 · Jan 11, 2014 at 10:41 PM 0
Share

The rest of the code appears to be C#, so I assume you want the translation?

 //With C# is makes more since to use a List ins$$anonymous$$d on an array, if you want to
 //Clear or Add/Remove from the Collection. JS arrays are dynamic, C# are not.
 private List<Weapon> _weapons = new List<Weapon>();
 public void ClearWeapons()
 {
     //Clear the List
     _weapons.Clear();
 }

 //Here is a direct and silly translation
 List<Weapon> _weapons = new List<Weapon>();
 public void Clear(){
     var Dweapons = new List<Weapon>(_weapons);
     Dweapons.Clear();
     weapons = Dweapons.ToArray();
 }

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Slobdell · Jan 11, 2014 at 10:25 PM

You're doing too much work. Replace

 pWeapons.ClearArray();

with

 pWeapons = [];

This will just set it to a brand new empty array, and you don't need your function at all.

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 z3nth10n · Jan 11, 2014 at 10:34 PM 0
Share

Unexpected symbol `['

avatar image agies1 · Jan 11, 2014 at 10:43 PM 0
Share

In C# it would be pWeapons = new Weapon[Size];

avatar image z3nth10n · Jan 11, 2014 at 10:48 PM 0
Share

The GameObject still appers... I tested that before...

avatar image z3nth10n · Jan 11, 2014 at 10:52 PM 0
Share

I'm thinking that the problem is in EquipWeapon:

http://pastebin.com/LEhcufTN

I'm checking it, but two heads are better than one.. :P

avatar image z3nth10n · Jan 18, 2014 at 12:26 AM 0
Share

Well, I actually know that the problem isn't in the code that I passed... Is in my Inventory system... When I scroll the mouse wheel the Slot freezes, and load the last loaded weapon...

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

Why don't my gameobjects load in when I export my game to webgl?,Why don't any of my objects render when I export my game to webgl? 0 Answers

Compilor Error using Dictonary collection 1 Answer

NaN exception when spawning soldiers 1 Answer

Weird GetComponent error 2 Answers

Why my object is ''destroyed'' when it's actually still in the scene? 0 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