- Home /
i need to copy a list from the collided to the newly instantiated one individually .
I need to copy a list from the collided to the newly instantiated one individually. But when 1 ball gets this value then everyone else also gets this list, when some amount of ball collide (trigger) a multiplayer the multiplayer will instantiate balls in the quantity of (amount of ball collided * 2) . I need to access every new ball created and add the name of the multilayer to a script with a public list variable called "Multiplayers". But when 1 ball collides with the multiplayer all the balls are adding the name of the collided multiplayer to the "Multiplayers" list . How to change the value of each newly instantiated balls suppurate
void newBall()
{
if (n > 0)
{
newBallobj = (GameObject) Instantiate(Ball , transformPos + new Vector3(0f,-0.5f,0f) , Quaternion.identity);
n--;
newBallobj.GetComponent<BallScript>().multiplayers = collided.GetComponent<BallScript>().multiplayers;
Invoke("newBall", 0.05f);
}
}
This is the full code if you need
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MultiplayerScript : MonoBehaviour
{
public int multiplayerCount;
public GameObject Ball;
int n;
Vector3 transformPos;
GameObject collided;
GameObject newBallobj;
private void Start()
{
n = 0;
}
private void OnTriggerEnter(Collider other)
{
if (other.CompareTag("Ball"))
{
collided = other.gameObject;
if ( !(collided.GetComponent<BallScript>().multiplayers.Contains(this.name)) )
{
Debug.Log(this.name);
collided.GetComponent<BallScript>().multiplayers.Add(this.name);
transformPos = other.transform.position;
n += multiplayerCount;
Invoke("newBall", 0.05f);
}
}
}
void newBall()
{
if (n > 0)
{
newBallobj = (GameObject) Instantiate(Ball , transformPos + new Vector3(0f,-0.5f,0f) , Quaternion.identity);
n--;
newBallobj.GetComponent<BallScript>().multiplayers = collided.GetComponent<BallScript>().multiplayers;
Invoke("newBall", 0.05f);
}
}
}
Comment