如何在 Unity3d 中使用相同的脚本为每个游戏对象设置不同的值变量
How to set different value variables for each game object using same script in Unity3d
我是 quiUnity 的新手,我正在尝试创建三个具有一个脚本的不同对象,并为每个实例设置一个 value 变量。
我已将脚本拖放到场景中的三个对象,并从统一 ui 插槽值变量中为每个对象设置不同的值!
当前代码如下:
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。
我是否应该为每个对象创建不同的脚本并将脚本分别分配给每个对象?这对我来说听起来不正确,我不知道为什么,但一定有什么!
您没有初始化 value
变量,因此它在附加到 3 个对象时变为 0
。即使你将它初始化为一个数字,它在每个实例上仍然是那个数字。
您需要一种方法来初始化每个 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;
}
}
如果您为每个球设置不同的值,它应该可以工作
我是 quiUnity 的新手,我正在尝试创建三个具有一个脚本的不同对象,并为每个实例设置一个 value 变量。
我已将脚本拖放到场景中的三个对象,并从统一 ui 插槽值变量中为每个对象设置不同的值!
当前代码如下:
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。
我是否应该为每个对象创建不同的脚本并将脚本分别分配给每个对象?这对我来说听起来不正确,我不知道为什么,但一定有什么!
您没有初始化 value
变量,因此它在附加到 3 个对象时变为 0
。即使你将它初始化为一个数字,它在每个实例上仍然是那个数字。
您需要一种方法来初始化每个 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;
}
}
如果您为每个球设置不同的值,它应该可以工作