我正在尝试为我触摸的每个对象设置不同的值变量,并且出于某种原因,我得到该对象为null - 就像没有初始化一样!
以下是代码:
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 != null)
{
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;
}
}
else {
this.txt.text = "object is null";
}
}
// 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
}
}
this.txt.text = this.value + "";
}
if (Input.GetMouseButtonUp(0)) {
draggingMode = false;
}
}
}
如何确保对象已初始化,并且我可以为public int value属性为每个对象设置不同的值?
编辑代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class DragBall : MonoBehaviour {
public GameObject gameObjectToDrag;
public Text txt;
private 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 () {
if (this.gameObject.name == "Ball1")
{
this.value = 1;
Debug.Log("BALL 1 VALUE SET TO " + value);
}
else if (this.gameObject.name == "Ball2")
{
this.value = 2;
Debug.Log("BALL 2 VALUE SET TO " + value);
}
else if (this.gameObject.name == "Ball3")
{
this.value = 3;
Debug.Log("BALL 3 VALUE SET TO " + value);
}
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse is pressed down");
Camera cam = Camera.main;
//Raycast depends on camera projection mode
Vector2 origin = Vector2.zero;
Vector2 dir = Vector2.zero;
if (cam.orthographic)
{
origin = Camera.main.ScreenToWorldPoint(Input.mousePosition);
}
else
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
origin = ray.origin;
dir = ray.direction;
}
RaycastHit2D hit = Physics2D.Raycast(origin, dir);
//Check if we hit anything
if (hit)
{
Debug.Log("We hit " + hit.collider.name);
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
}
}
}
if (Input.GetMouseButtonUp(0))
{
draggingMode = false;
}
}
}