C# 如何使用Unity3d中的相同脚本为每个游戏对象设置不同的值变量
我是Unity的新手,我正在尝试创建三个不同的对象,它们有一个脚本,并为每个实例设置一个值变量 我已经将脚本拖放到场景中的三个对象上,并将unity ui slots值变量设置为每个对象的不同值 当前代码如下所示:C# 如何使用Unity3d中的相同脚本为每个游戏对象设置不同的值变量,c#,unity3d,game-engine,unity3d-2dtools,C#,Unity3d,Game Engine,Unity3d 2dtools,我是Unity的新手,我正在尝试创建三个不同的对象,它们有一个脚本,并为每个实例设置一个值变量 我已经将脚本拖放到场景中的三个对象上,并将unity ui slots值变量设置为每个对象的不同值 当前代码如下所示: using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class DragBall : MonoBehaviour {
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代表所有人
我是否应该为每个对象创建不同的脚本,并将脚本分别分配给每个对象?对我来说,这听起来并不正确,我知道为什么,但肯定有什么东西 您没有初始化值变量,因此它在附加到3个对象时变为0。即使您将其初始化为一个数字,在每个实例上仍然是这样
您需要一种方法来初始化每个游戏对象上的值变量。要做到这一点,您还需要一种方法来区分每个游戏对象。这可以通过比较游戏对象、标签、图层或游戏对象实例的当前名称来实现
下面的示例使用游戏对象的名称来初始化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;
}
}
如果您在inspector中将该值设置为不同的数字,则该脚本应该可以工作,并在日志中显示不同的数字。我能看到的是,每次更新都会运行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;
}
}
如果您为每个球设置了不同的值,那么它应该可以工作
我添加了你所说的,仍然不起作用,我现在怀疑我是否正确地将脚本设置到每个obejct中,我所做的只是将脚本文件拖放到每个obejct中,将每个对象分配到gameObjectToDrag,并为每个对象设置不同的值变量,仍然没有区别这是非常简单的事情。为什么不把这3个游戏对象中的每一个都拿出来,然后把链接贴在评论部分。我想看看。另一个控制台选项卡日志的屏幕截图显示它仍然显示0。最后,更新您的问题并在其下进行编辑,然后包括新代码,该代码显示了我的答案中的实现。不要删除原始代码。只需添加编辑+新代码。你做错了什么,但我需要这些来找出它是什么。我认为你是对的,我只是需要将你的代码移到更新方法中,这样现在我就可以看到文本标签根据我拖动的对象的不同而改变为一个值!我会检查你的答案是否正确!感谢您在这方面的帮助1在开始功能中效果更好,否则您稍后尝试修改时会遇到问题,因为更新功能将继续运行。该代码应该只运行一次。很高兴你成功了!除非你们有更多的问题,或者我将要发表的评论没有解决问题,否则你们不必提出新的问题。将三个类似的if this.gameObjectToDrag.name==Ball1{this.value=1;}语句用ifthis.gameObjectToDrag!=无效的这将确保在使用此.gameObjectToDrag.name…之前gameObjectToDrag不为null。。。。