0
\$\begingroup\$

I'm trying to clone items inside array. But the clones i'm getting is bigger than the original one!! Also, after couple of second the script is missing/removing some clones !!

Update 1: I'm working on puzzle game such as "Candy Crush". Now I have my items to fill inside sorts"dots". What script should do, Clone my items and insert them on sort place. What happen is I get the size bigger than the original one. and after seconds script is missing/removing some clones from "Clone_Ghost" !!

public GameObject[] Items; // 4 elements
public GameObject[] dots; // where to place my clones
public GameObject ClonePlace; // hold inside canvas
public GameObject[] Clone_Ghost; //


void Start()
{
    
    Clone_Ghost = new GameObject[dots.Length];

    for (int i = 0; i < dots.Length; i++)
    {           
        Clone_Ghost[i] = Instantiate(Items[Random.Range(0,3)], dots[i].transform.position, dots[i].transform.rotation);
        Clone_Ghost[i].transform.parent = ClonePlace.transform;
    }
    
}

Update: Add this line at end to solve the scale problem.

    Clone_Ghost[i].transform.localScale = new Vector3(1, 1, 1);

and about the "missing/removing" is because another script that work to destroy matches items.

\$\endgroup\$
  • \$\begingroup\$ I'm not able to reproduce this problem using only the code you've shown here. Please edit your question to walk us through every step we need to follow to reproduce this problem in a new, empty project. Be sure to look at details like the scale values of your dots and ClonePlace objects. \$\endgroup\$ – DMGregory Oct 16 at 18:49
  • \$\begingroup\$ I wrote more details now. Could you figure any solution now? \$\endgroup\$ – user43474 Oct 16 at 19:00
  • \$\begingroup\$ Try this exercise: close your project and make a new one. Without looking at your old project, using only the text you've written here, can you make this same problem occur again? If you have to do anything not described in the text above, edit your question to include that step too. \$\endgroup\$ – DMGregory Oct 16 at 19:02
  • \$\begingroup\$ It seems likely that your ClonePlace has some scaling going on. Is that so? \$\endgroup\$ – Weckar E. Oct 16 at 19:33
  • \$\begingroup\$ Yes. I solve it now. I added this "Clone_Ghost[i].transform.localScale = new Vector3(1, 1, 1);" line at end and I got my size. \$\endgroup\$ – user43474 Oct 16 at 19:39
0
\$\begingroup\$

Here is how to solve the scale problem.

public GameObject[] Items; // 4 elements
public GameObject[] dots; // where to place my clones
public GameObject ClonePlace; // hold inside canvas
public GameObject[] Clone_Ghost; //


void Start()
{

Clone_Ghost = new GameObject[dots.Length];

for (int i = 0; i < dots.Length; i++)
{           
    Clone_Ghost[i] = Instantiate(Items[Random.Range(0,3)], dots[i].transform.position, dots[i].transform.rotation);
    Clone_Ghost[i].transform.parent = ClonePlace.transform;
    Clone_Ghost[i].transform.localScale = new Vector3(1, 1, 1);
}

}
| improve this answer | |
\$\endgroup\$

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.