1

I am currently making a memory game where it spawns a 3D object into the scene and checks if the 2 game object is the same. So currently, my scripts is able to do the checking by using the name of the gameobject and able to spawn the amount of gameobject based on the amount of textures/material that is in the list but I have run into a problem where 3 or more of the same texture of the gameobject have spawn, which I was thinking of using Boolean to limit the amount of gameobject being spawn with the same texture, but I do not know how I should implement it into the code. By the way, with the current code, the gameobject spawns at the same location which I have not got into, as I was stuck due to having the current issue.

public class CardControl : MonoBehaviour
{
public List<Material> materialList;
public GameObject prefab;
private Vector3 pos;
private Quaternion rot = Quaternion.identity;
private bool Same;

private float XPosition = 0f;
private float YPosition = 0f;
private float ZPosition = 0f;

private void Awake()
{
    pos = new Vector3(XPosition, YPosition, ZPosition);
    Same = false;


    for (int i = 0; i < (materialList.Count * 2); i++)
    {
        Material mat = RandomMaterial(materialList);
        
        InstantiateWithMaterial(prefab, pos, rot, mat);
        
    }
}

public Material RandomMaterial(List<Material>_List_)
{

    return _List_[Random.Range(0, _List_.Count)];
    
}

public void InstantiateWithMaterial(GameObject _prefab_, Vector3 _pos_, Quaternion _rot_, Material _mat_)
{
    GameObject obj_ = Instantiate(_prefab_, _pos_, _rot_);
    obj_.gameObject.GetComponent<MeshRenderer>().material = _mat_;
    obj_.name = _mat_.name + "1";
}
}
unco
  • 11
  • 1

1 Answers1

0

I would make those materials in array, and second array of integers. Then take material with random index from material array.If integer with same index in integers array is < amount_of_spawned_materials you want use it and increase the integer, otherwise randomise again.

Tomasz
  • 199
  • 5