- Home /
Question by
Matasiethe · Mar 21, 2017 at 03:21 AM ·
prefab-instance
Change one intance, change all the prefab - Not found the correct answer in other threads
I'd read some similar questions, but no one help me. I have a prefab that instanciate regular times, when I click over a singular prefab, the gameobject turn green, change a bool and play an animation. The problen is that all the prefab made these three things. What can I do? This is the script of the instance:
public class GameManager : MonoBehaviour
{
public GameObject [] piso; //prefab cuadrado
public GameObject cuadrados; //gameobject para poner los cuadrados generados
public float medidaZ = 50f; //medida para agregar el otro cuadrado
private int contador = 0; //contador de cuadrados
private GameObject pisos;
// Use this for initialization
void Start ()
{
GenerarCuadrados();
GenerarCuadrados();
GenerarCuadrados();
GenerarCuadrados();
GenerarCuadrados();
}
public void GenerarCuadrados()
{
pisos = Instantiate(piso[Random.Range(0, piso.Length)], new Vector3(transform.position.x, transform.position.y, medidaZ), transform.rotation) as GameObject;
pisos.transform.SetParent(cuadrados.transform);
pisos.name = (contador + 1).ToString();
contador++;
medidaZ = transform.position.z + medidaZ + 10f;
}
}
And this is the script of the prefab:
{
private bool controlClick; //bool para controlar si el jugador hizo click en el cuadrado.
public Material material; //variable para cambiar el color del cuadrado cuando se toque.
public Material materialInicial; //variable con el color inicial
public AudioSource sonidoAltocar; //sonido cuando tocas el cuadrado y se vuelve verde.
private bool cuadradoVerdeOK = false; //bool para saber si se toco el cuadrado y se volvio verde
public Animator anim; //animacion de que gira el piso cuando es tocado
private GameObject[] piso;
void Start()
{
material = new Material(material);
cuadradoVerdeOK = false; //empieza siempre en falso, no es verde.
gameObject.GetComponent<MeshRenderer>().material = materialInicial;
controlClick = false;
}
void Update()
{
//cuando toca el cuadrado este cambia el color y suena
if (Input.GetMouseButton(0) && !cuadradoVerdeOK)
{
anim.Play("clickPiso");
GetComponent<MeshRenderer>().material = new Material(material);
sonidoAltocar.Play();
cuadradoVerdeOK = true;
controlClick = true; //indica que el jugador ha hecho click
}
}
Comment
Your answer
Follow this Question
Related Questions
Why can't I undo changes to a prefab? (or to an instance of it after I apply the changes) 1 Answer
Unity 2D Game Issue: Script is only affecting one prefab instead of all of them. 0 Answers
How to undo prefab Apply? 0 Answers
I need to move a clone of a prefab to a specific position after it instantiates 1 Answer
How do I get the Player's Health Script as Component? 1 Answer