[英]How to set different value variables for each game object using same script in Unity3d
我对Unity还是很陌生,我试图创建三个具有一个脚本的不同对象,并为每个实例设置一个值变量。
我已将脚本拖放到场景中的三个对象上,并从unity ui插槽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。
是否应该为每个对象创建不同的脚本并将脚本分别分配给每个对象? 这对我idk来说不仅仅听起来是正确的方法,但是必须有一些东西!
您没有初始化value
变量,因此当附加到3个Object时它变为0
。 即使将其初始化为数字,每个实例上仍将是该数字。
您需要一种方法来初始化每个GameObject
上的value变量。 为此,您还需要一种区分每个GameObject
。 这可以通过比较GameObject的当前名称,标签,图层或GameObject实例来完成。
下面的示例使用GameObject的名称在Start
函数中初始化value
变量。 假设它们分别命名为“ Obj1”,“ Obj2”和“ Obj3”:
void Start()
{
//Use 1 for Object 1
if (this.gameObject.name == "Obj1")
{
value = 1;
}
//Use 3 for Object 2
else if (this.gameObject.name == "Obj2")
{
value = 2;
}
//Use 3 for Object 3
else if (this.gameObject.name == "Obj3")
{
value = 3;
}
}
如果您在检查器中将值设置为其他数字,则此脚本应该可以工作并在日志中显示其他数字。 我看到的是您每次更新都运行txt.text = this.value
。 如果您有一个文本字段连接到球的所有实例,则它将始终显示相同的值。 如果要让文本字段显示当前被拖动的球,则应将该代码行放入if语句中,如下所示:
if (Input.GetMouseButtonDown(0)) {
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray, out hit))
{
//Add line here
txt.text = this.value + "";
gameObjectToDrag = hit.collider.gameObject;
GOcenter = gameObjectToDrag.transform.position;
touchPosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
offset = touchPosition - GOcenter;
draggingMode = true;
}
}
如果您为每个球设置了不同的值,那么它应该起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.