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);
}
}
4 Answers
4
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
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 _= original*;*_
_*}*_
_*finalArray[finalArray.Length - 1] = itemToAdd;*_
_*return finalArray;*_
_*}*_
_*}*_
_*```*_
_*<p>So as you can see, it isn't very easy to resize arrays.</p>*_
_*<p>Or, as Mike said right before I posted, you should probably just use a List. It will handle this for you.</p>*_
T AddtoArray(T Org, T New_Value)
{
T New = new T[Org.Length + 1];
Org.CopyTo(New, 0);
New[Org.Length] = New_Value;
return New;
}
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);
}
}
}
}
}
Thank you for your response it was very helpful!
– anon39953356