- Home /
C# adding items to an already created Array
Hello Unity Community,
I am having a really hard time trying to create a piece of code that would add objects that I would select into an array. I was doing some research and it said that I would only be able to do the array.Add or array.Push in java. Which sucks because our project is in C#, is there any alternative that I could possible look into for this problem Thanks, Nolan Encarnacion
using UnityEngine; using System.Collections;
public class SelectionManager : MonoBehaviour { public RaycastHit hit; public Ray ray; public GameObject[] selection; // Use this for initialization
// Update is called once per frame
void Update ()
{
if ( Input.GetMouseButtonDown(0) )
{
ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray, out hit, 100))
{
addtoArray(hit.collider.gameObject);
Debug.Log(selection);
//Debug.Log(hit.collider.gameObject.name);
}
}
}
void addtoArray(gameObject obj)
{
selection.Add(obj);
}
}
Answer by Mike 3 · Apr 07, 2011 at 11:21 PM
You generally want to use List for this kind of thing
Add this to the top of the file:
using System.Collections.Generic;
then this instead of your array:
List<GameObject> selection;
Add should work fine then
Thank you for your response it was very helpful!
Answer by Peter G · Apr 07, 2011 at 11:23 PM
No, you have to recreate the array with its new member. You may as well make it a generic method:
using System.Collections.Generic;
public static class Extensions {
public static T[] AddItemToArray <T> (this T[] original, T itemToAdd) {
T[] finalArray = new T[ original.Length + 1 ];
for(int i = 0; i < original.Length; i ++ ) {
finalArray[i] = original[i];
}
finalArray[finalArray.Length - 1] = itemToAdd;
return finalArray;
}
}
So as you can see, it isn't very easy to resize arrays.
Or, as Mike said right before I posted, you should probably just use a List. It will handle this for you.
yeah I think I'm just going to use a list. $$anonymous$$UCH easier haha! Thank you though for your response. It is much appreciated.
Answer by V-Jankaitis · Jun 27, 2018 at 02:06 PM
T[] AddtoArray<T>(T[] Org, T New_Value)
{
T[] New = new T[Org.Length + 1];
Org.CopyTo(New, 0);
New[Org.Length] = New_Value;
return New;
}
No, he didn't. The max valid index is always "Length-1". Since the new length is actually the old length +1 he can simply use the old length as index into the new array.
Example
// length of array: 4
// index 0 1 2 3
// value A B C D
// new array length: 4+1 == 5
// index 0 1 2 3 4
// value A B C D New
Hmm.. effectively recycling Org.Length instead of writing New.Length-1 I think I get it. Thanks for the explanation.
Answer by mx_official · Aug 11, 2020 at 10:17 AM
If anyone needed the same script but for models with multiple materials, here you go.
edit: Made fadePerSecond serialized for others who will be using the script in their own way.
using System.Collections.Generic;
using UnityEngine;
public class FadeAlpha : MonoBehaviour
{
[SerializeField] private float fadePerSecond = 0;
[SerializeField] private List<Material> materials = new List<Material>();
[SerializeField] private bool moreThanOneMat = false;
void Start()
{
// Locating Materials
if (GetComponent<MeshRenderer>().materials.Length > 1)
{
moreThanOneMat = true;
foreach (Material m in GetComponent<MeshRenderer>().materials)
{
materials.Add(m);
}
}
}
void Update()
{
if (moreThanOneMat == true)
{
DropAlphaForAll();
}
else
{
var material = GetComponent<MeshRenderer>().material;
var color = material.color;
if (color.a > 0)
{
material.color = new Color(color.r, color.g, color.b, color.a - (fadePerSecond * Time.deltaTime));
}
if (color.a < 0)
{
material.color = new Color(color.r, color.g, color.b, 0);
}
}
}
void DropAlphaForAll()
{
if (GetComponent<MeshRenderer>().materials.Length > 1)
{
foreach (Material mats in materials)
{
if (mats.color.a > 0)
{
mats.color = new Color(mats.color.r, mats.color.g, mats.color.b, mats.color.a - (fadePerSecond * Time.deltaTime));
}
if (mats.color.a < 0)
{
mats.color = new Color(mats.color.r, mats.color.g, mats.color.b, 0);
}
}
}
}
}
Your answer
Follow this Question
Related Questions
Instantiate same PreFab to an array of GameObject by RayCast. 1 Answer
NullReferenceError, Tilemap Array with Transforms and Raycast (C# with Demo) 0 Answers
Change multiple object layers 0 Answers
C# Raycast Prefab script activation 1 Answer
How to change colour of Game objects when hit by ray cast using array 0 Answers