- Home /
Question by
rahulpatil6220375 · May 02, 2019 at 09:34 AM ·
instantiatecube
Problem with repeating objects in array when already in the array
When the player move forward then paint is instantiate and stored in the array, even if the paint is already in the array it will again be stored in the array.
But I don't want to repeat any paint in the array.
public GameObject[] array;
public GameObject paint;
int temp = 0;
private RaycastHit hit;
public float speed = 10f;
public Text levelText;
public GameObject paint;
float maxdistance = 0.51f;
Vector3 newtargetposition;
bool pos;
private void FixedUpdate()
{
if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forw ard,out hit, maxdistance))
{
if (hit.collider.gameObject.tag == "Obstacle")
{
move = false;
}
if (move)
{
PlayerMove();
}
}
}
public void PlayerMove()
{
if (move)
{
vector3 positions = transform.position + new Vector3(0f, -0.5f,0f); //when player move then paint instantiate y position
array[temp] = Instantiate<GameObject>(paint, position, Quaternion.identity);
temp++;
newtargetposition=position;
Debug.Log("newtargetposiiton:" + newtargetposition);
if (temp == 150)
{
if (newtargetposition == position)
{
//what can i do here
//i dont want to paint here because position store in newtargetposition,it is repeat
//array[temp]--;
Debug.Log("newtargetpositions:" + newtargetposition);
//temp--;
}
gameOver.SetActive(true);
SceneManager.LoadScene(1);
}
}
}
when player move forward
when player move backword
How can I remove repeated instantiation of the object and repeated objects in the array?
backmove.png
(190.9 kB)
playerforward.png
(184.4 kB)
Comment
Use HashSet to store positions. HasSet provides uniqeness. It won't add same Vector3 value into collection.
Why not before instantiate, just check if it already exists?
if (!exists) Instantiate();
Bye :D
can u edit my code????plz...
public GameObject[] array;
public GameObject paint;
int temp = 0;
private RaycastHit hit;
public float speed = 10f;
public Text levelText;
public GameObject paint;
float maxdistance = 0.51f;
Vector3 newtargetposition;
bool pos;
HashSet<Vector3> paintPositions;
private void Awake(){
paintPositions = new HashSet<Vector3>();
}
private void FixedUpdate() {
if(Physics.Raycast(transform.position,transform.TransformDirection(Vector3.forw ard,out hit, maxdistance))
{
if (hit.collider.gameObject.tag == "Obstacle")
{
move = false;
}
if (move)
{
Player$$anonymous$$ove();
}
}
}
public void Player$$anonymous$$ove()
{
if (move)
{
vector3 positions = transform.position + new Vector3(0f, -0.5f,0f); //when player move then paint instantiate y position
if(!paintPositions.Contains(positions)) {//if position doesn't already exist
array[temp] = Instantiate<GameObject>(paint, position, Quaternion.identity);
temp++;
newtargetposition=position;
Debug.Log("newtargetposiiton:" + newtargetposition);
}
if (temp == 150)
{
if (newtargetposition == position)
{
//what can i do here
//i dont want to paint here because position store in newtargetposition,it is repeat
//array[temp]--;
Debug.Log("newtargetpositions:" + newtargetposition);
//temp--;
}
gameOver.SetActive(true);
Scene$$anonymous$$anager.LoadScene(1);
}
}
}
I just added the mechanism to control instantiate phase. Your code needs to be revised though.
Your answer
![](https://koobas.hobune.stream/wayback/20220612200555im_/https://answers.unity.com/themes/thub/images/avi.jpg)