- Home /
How to change array length
How to i can change array length.
public GameObject[] items;
void OnMouseDown(){ items.Length-=1; }
it's possible?
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.
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...
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.
I'm using List class and it's beautiful as an inventory system.
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;
}
}
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.
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?
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
@Nirvana33 what if you make a very long array, and make an empty gameobject that when equipped is not shown because it represents nothing.
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,
Your answer
![](https://koobas.hobune.stream/wayback/20220613110333im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
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