- Home /
dont understand copy a collision on an array, i want to store data in the array, change it but not the gameobject that collide... help
if in OntriggerEnter i copy collision in my array, after if a i modify my array i modify the gameobject that collide with me, i try it in the fuction called "seleziona bersaglio". how can i only have all the information from collision? i want to do an array full of collision and after if they exit put for example the name=null. but only in the array... help please. thanks in advance.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TargetTower : MonoBehaviour
{
int i = 0, j = 0, k = 0;
private Collider2D[] Bersagli= new Collider2D[100];
private Collider2D bersaglio;
void Start()
{
Bersagli = new Collider2D[100];
}
void Update()
{
selezionaBersagio();
if (i > 0) {
Debug.Log("bersaglio: "+bersaglio.transform.name);
for (int z = 0; z < i; z++)
{
Debug.Log("indice:"+z+"= "+Bersagli[z].transform.name);
}
}
}
private void selezionaBersagio()
{
if (bersaglio == null)
{
bersaglio = Bersagli[0];
bersaglio.transform.position = new Vector3(-100f, 100f, 10f); **//this move my gameobject, i did it to test and this is my problem**
}
}
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag=="giocatore")
{
Bersagli[i] = collision.GetComponent<Collider2D>();
`` i++;
``//**i take the collider2d and i want to copy in my array to have all the collision. i dont want change the game object that collide.**
}
}
private void OnTriggerExit2D(Collider2D collision)
{
if (collision.tag == "giocatore")
{
for (j = 0; j < i; j++) //elimina e compatat array, lista di tutti i bersagli, se esce lo tolgo dalla lista
{
if (collision.name == Bersagli[j].name)//cerco il nome
{
Bersagli[j].name = null;//lo metto a null
for ( k = 0; k < i; k++) //scorro tutti componenti
{
if (Bersagli[k]==null)//quando trovo il null che ho messo
{
for (int l = k; l < i; l++) //copio il successivo nel precedente
{
Bersagli[k] = Bersagli[k + 1];
}
}
}
i--;
}
}
}
}
}
i am having issues to understand your explanationm, you dont have an array of collisions you have an array of colliders, what do you mean by collision information?
thanks for answer me i have fixed with:
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.tag=="giocatore")
{
40. Bersagli[i] = new Bersaglio();
Bersagli[i].name= collision.GetComponent<Transform>().name;
Bersagli[i].position = collision.GetComponent<Transform>().position;
`` i++;
my answer was why if i do :
Bersagli[i] = collision.GetComponent();
after if i i do for example:
Bersagli[i].name=null;
why if i change the name in my array i change the name of the real compenent? i did a copy of the component in my array...
oh! no when you get componentes you get a reference to them not a copy, for example if you save the transform of the object in the array and changethe position/rotation of that transform in the array it would also change the position and rotation of the object, the same goes to most of the componentes (light, mesh, rigidbody...)
Your answer
Follow this Question
Related Questions
KEEP INSTANTIATE PREFAB ONMOUSEDOWN 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
My script only partially works 2 Answers
Values not moving to Game Over screen or the Game over doesn't somehow work. 0 Answers