using TMPro;
public class InstanziaOggetti : MonoBehaviour
{
public ObjectScriptable[] ObjectX;//name of the scriptableObject
public TMP_Text text;//3d text that will be instantiated above the object
public TMP_Text[] objectName;//name that will be written of the instantiated object
void Start()
{
ButtonName();
}
public void Button1() //with the n1 button I instantiate the first object
{
DestroyObjectInScene();
CreateNewObject(0);
}
public void Button2()//with the n2 button I instantiate the second object
{
DestroyObjectInScene();
CreateNewObject(1);
}
public void Button3()
{
DestroyObjectInScene();
CreateNewObject(2);
}
public void CreateNewObject(int objectNumber)//create the object to instantiate
{
ObjectScriptable ob = ObjectX[objectNumber];
GameObject objectY = Instantiate(ob.objectPrefab);
objectY.transform.position = new Vector3(0, 0.5f, 0);
TMP_Text tt = Instantiate(text);
tt.transform.position = new Vector3(0, 2.5f, 0);
tt.text = ob.objectName;
}
public void DestroyObjectInScene()//destroy stage objects that have the Respawn tag
{
GameObject objectInScene = GameObject.FindGameObjectWithTag("Respawn");
if (objectInScene != null)
{
Destroy(objectInScene);
}
}
void ButtonName() //the name of the object is associated with the 3D text
{
for (int i = 0; i < ObjectX.Length; i++)
{
if (ObjectX[i] != null)
{
objectName[i].text = ObjectX[i].objectName;
}
else
{
objectName[i].text = "";
}
}
}
}
У меня возникла проблема с этим скриптом, и я не знаю, была ли у меня ошибка в коде или в Unity.
Скрипт должен отображать массивы объектов, доступных для сценариев. . Не могу найти причину, почему объекты не появляются после нажатия кнопок
Исправьте мой скрипт или пришлите его улучшенную версию
[code]using TMPro; public class InstanziaOggetti : MonoBehaviour { public ObjectScriptable[] ObjectX;//name of the scriptableObject public TMP_Text text;//3d text that will be instantiated above the object public TMP_Text[] objectName;//name that will be written of the instantiated object void Start() { ButtonName(); } public void Button1() //with the n1 button I instantiate the first object { DestroyObjectInScene(); CreateNewObject(0); } public void Button2()//with the n2 button I instantiate the second object { DestroyObjectInScene(); CreateNewObject(1); } public void Button3() { DestroyObjectInScene(); CreateNewObject(2); } public void CreateNewObject(int objectNumber)//create the object to instantiate { ObjectScriptable ob = ObjectX[objectNumber]; GameObject objectY = Instantiate(ob.objectPrefab); objectY.transform.position = new Vector3(0, 0.5f, 0);
TMP_Text tt = Instantiate(text); tt.transform.position = new Vector3(0, 2.5f, 0); tt.text = ob.objectName; } public void DestroyObjectInScene()//destroy stage objects that have the Respawn tag { GameObject objectInScene = GameObject.FindGameObjectWithTag("Respawn"); if (objectInScene != null) { Destroy(objectInScene); } } void ButtonName() //the name of the object is associated with the 3D text { for (int i = 0; i < ObjectX.Length; i++) { if (ObjectX[i] != null) { objectName[i].text = ObjectX[i].objectName; } else { objectName[i].text = ""; } } } } [/code] У меня возникла проблема с этим скриптом, и я не знаю, была ли у меня ошибка в коде или в Unity. Скрипт должен отображать массивы объектов, доступных для сценариев. . Не могу найти причину, почему объекты не появляются после нажатия кнопок Исправьте мой скрипт или пришлите его улучшенную версию