Я новичок в Unity и пытаюсь создать три разных объекта с одним скриптом и установить переменную value для каждого экземпляра.
Я перетащил скрипт к трем объектам в моей сцене и установил из переменной значения слотов единства пользовательского интерфейса разные значения для каждого объекта!
Текущий код выглядит так:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DragBall : MonoBehaviour {
public GameObject gameObjectToDrag;
public Text txt;
public int value;
public Vector3 GOcenter;
public Vector3 touchPosition;
public Vector3 offset;
public Vector3 newGOcenter;
RaycastHit hit;
public bool draggingMode = false;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
gameObjectToDrag = hit.collider.gameObject;
GOcenter = gameObjectToDrag.transform.position;
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
offset = touchPosition - GOcenter;
draggingMode = true;
}
}
if (Input.GetMouseButton(0)) {
if (draggingMode) {
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
newGOcenter = touchPosition - offset;
gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 3, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
}
}
if (Input.GetMouseButtonUp(0)) {
draggingMode = false;
}
Debug.Log(this.value);
txt.text = this.value + "";
}
}
Отредактированный код:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DragBall : MonoBehaviour {
public GameObject gameObjectToDrag;
public Text txt;
public Vector3 GOcenter;
public Vector3 touchPosition;
public Vector3 offset;
public Vector3 newGOcenter;
RaycastHit hit;
public int value;
public bool draggingMode = false;
// Use this for initialization
void Start () {
if (this.gameObjectToDrag.name == "Ball1")
{
this.value = 1;
}
else if (this.gameObjectToDrag.name == "Ball2")
{
this.value = 2;
}
else if (this.gameObjectToDrag.name == "Ball3")
{
this.value = 3;
}
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
this.gameObjectToDrag = hit.collider.gameObject;
GOcenter = this.gameObjectToDrag.transform.position;
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
offset = touchPosition - GOcenter;
draggingMode = true;
}
}
if (Input.GetMouseButton(0)) {
if (draggingMode) {
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
newGOcenter = touchPosition - offset;
if (this.gameObjectToDrag.name == "Ball1") {
this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 1, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
}
else if (this.gameObjectToDrag.name == "Ball2")
{
this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 2, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
}
else if (this.gameObjectToDrag.name == "Ball3")
{
this.gameObjectToDrag.transform.position = new Vector3(newGOcenter.x, 3, newGOcenter.z); // 0 means Y axis , so let the ball go horizontally only
}
}
txt.text = this.value + "";
}
if (Input.GetMouseButtonUp(0)) {
draggingMode = false;
}
}
}
Я хотел бы, чтобы для каждого объекта, к которому я прикасаюсь мышью, отображалась переменная текущего значения. Сейчас показывает 0 для всех.
Должен ли я создавать разные сценарии для каждого объекта и назначать сценарий каждому объекту отдельно? это не просто звучит правильно для меня, не знаю почему, но что-то должно быть!