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
1
Question by Nirvana33 · Jul 18, 2013 at 12:39 AM · arraylength

How to change array length

How to i can change array length.

public GameObject[] items;

void OnMouseDown(){ items.Length-=1; }

it's possible?

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

4 Replies

· Add your reply
  • Sort: 
avatar image
9

Answer by aldonaletto · Jul 18, 2013 at 02:06 AM

As @Slobdell said, built-in arrays can't be directly resized - but you can do it the hard way:

 GameObject[] temp = new GameObject[newSize];
 items.CopyTo(temp, 0);
 items = temp;

This code creates a temporary array temp with the desired size and copy to it the elements of items, then assign temp to items, effectively changing the size of the items array.

But if you need to change the array size very frequently (every frame, for instance), it's better to use the List class.

Comment
Add comment · Show 3 · 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 Slobdell · Jul 18, 2013 at 02:23 AM 0
Share

In that situation with a fixed amount of items a person can carry you may want to stick with an array and just not size it. That way if the array is full they can't pick up any more items and if the array slot is empty or null you just would display a blank in the game. If I understand what you're doing...

avatar image aldonaletto · Jul 18, 2013 at 02:47 AM 0
Share

Yes: if the amount of items is limited, just use an array with the right size and assign null to the empty elements.

But usually there's no need to save references to the objects currently in the inventory: the easiest way is to declare a class with variables that just tell how many items you have (or whether you have some item). For instance:

 public class Inventory {
   public bool hasRifle; // player has a rifle?
   public bool hasShotGun; // player has a shotgun?
   public int bullets; // how many bullets player has
   public int shells; // how many shells player has
   public int healthPacks; // how many health packs
 }
 
 Inventory inventory = new Inventory();

When the player picks a rifle, for instance, the script should set inventory.hasRifle to true; if it picks a box with 5 shells, the script adds 5 to inventory.shells, and so on.

avatar image Dothackking · Jul 18, 2013 at 02:54 AM 0
Share

I'm using List class and it's beautiful as an inventory system.

avatar image
-1

Answer by TimeLineStudio · Sep 03, 2021 at 10:57 PM

----------------This code is work--------

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class CameraManager : MonoBehaviour
 {
    public int ElementValue = -1;
    public Vector3 CameraPosition;
    public Vector3[] items = new Vector3[1];
    public Vector3[] items_Backup=new Vector3[1];
 
     // Start is called before the first frame update
     public void Record()
     {
         CameraPosition = Camera.main.transform.position;
     }
     public void SaveElement()
     {
         ElementValue++;
         items_Backup = new Vector3[ElementValue+1];
         items.CopyTo(items_Backup, 0);
         items = new Vector3[ElementValue];
         items = items_Backup;
         items[ElementValue] = CameraPosition;
     }
 
 }
Comment
Add comment · Show 1 · 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 Bunny83 · Sep 04, 2021 at 12:08 AM 2
Share

The question is over 8 years old and the OP hasn't been online on UA for 6 years. So this bump is pretty pointless. Apart from that your code makes not much sense. Your line 11 is pointless. You create a new array and immediately replace the array with the one you created in line 9. So that new array is immediately up for garbage collection. You never use this array at all, so you just waste memory. When you remove that pointless line your code looks exactly like the code that aldonaletto posted 8 years ago.


Regardless of all those issues, it's pretty pointless to use arrays this way. This is in general a bad usage. You should simply use a List. A List wraps an array internally but it's only resized / replaced when necessary. The internal array can be larger than the actual element count. So a List is simply an array and an additional count variable which does automatically grow when necessary.

avatar image
0

Answer by Slobdell · Jul 18, 2013 at 12:59 AM

No, arrays are immutable. I'm not sure what language you're using but in c# you can use a list and it is sizeable. Grows when you add and shrinks when you remove. But in your particular case it seems strange what you're doing resizing the array without doing anything to its contents. What are you trying to do?

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 Nirvana33 · Jul 18, 2013 at 01:56 AM 0
Share

i wanna make an inventory system like skyrim. Items attached to items[] array. When an item dropped, items array length must -1. But i cant do. What is other ways? sorry for bad English

avatar image samf1111 Nirvana33 · Mar 09, 2020 at 12:43 PM 0
Share

@Nirvana33 what if you make a very long array, and make an empty gameobject that when equipped is not shown because it represents nothing.

avatar image
0

Answer by deba124 · Jun 25, 2019 at 04:14 PM

Well, you dont need to worry about this. static int value; GameObject[] newArr;

void anyFunction() { newArr = new GameObject[value]; }

//Any function can change array length,

Comment
Add comment · 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

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

23 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

Related Questions

Assets/Gravitation_Simulator.cs(23,47): error CS1061: Type `UnityEngine.GameObject' does not contain a definition for `Length' and no extension method `Length' of type `UnityEngine.GameObject' could be found. 2 Answers

variables based on array.length not updating in JS 1 Answer

How to add an object to an array? 1 Answer

Why is this array out of length? 1 Answer

my2dArray.Length(0) 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