I am making multiple instances of prefab that has a script asign to it. In the script I am simply changing its color on mousedown. But clicking on one instance of prefab in the scene makes all other instances to change. I tried to change the material, so that each instance has its own material, but it did not seem to help. By logging I found out, that the method called on mousedown gets call for each instance. Could anyone explain this behaviour to me? I attached the code of script of the prefab and also code that I instantiate the prefabs with.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class markTile : MonoBehaviour
{
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit))
{
Debug.Log(GetComponent().transform.position+"clicked prefab");
Color currentEditorColor = EditorController.getEditorController().getCurrentEditorColor();
GetComponent().material.color = currentEditorColor;
}
}
}
}
public void CreateTileMap()
{
for(int i=0; i().material = material;
}
}
}
↧