0
\$\begingroup\$

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.forward,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 store position in array

enter image description here

when player move back already fillup position again store in array

enter image description here

\$\endgroup\$
  • \$\begingroup\$ This looks like it can be solved by the same answer as your previous question. Once you have a grid that tracks covered & uncovered cells, you can check whether a particular cell is covered already before you try to spawn new paint to cover it. \$\endgroup\$ – DMGregory May 2 at 11:35
  • \$\begingroup\$ thanks reply but i created 50 level,and each level different home, it is possible a grid tracks covered or not?? i m try another concept like hashset but it is not work? still not solved my problem \$\endgroup\$ – Rahul Viaxor May 2 at 11:40
  • \$\begingroup\$ Yes, it is possible - what fundamental law of the universe would render it impossible? \$\endgroup\$ – DMGregory May 2 at 11:41
  • \$\begingroup\$ i m use gizmos->selection grid but i m totally blank..which value pass in inspector width,height,row,column i have no idea \$\endgroup\$ – Rahul Viaxor May 2 at 11:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.