【发布时间】:2019-02-25 14:05:50
【问题描述】:
好的,这么快的问题,我做了一个简单的:
Ray ray;
然后在update()中我做了一个简单的:
ray = Camera.current.ScreenPointToRay(Input.mousePosition);
Debug.Log(Camera.current.ScreenPointToRay(Input.mousePosition));
由于某种原因,在控制台中,debug.log 注册了一个正在投射的射线,而射线只是认为它是空的。
有什么想法吗?
这是 debug.log 输出:
原点:(-0.2, 2.5, 14.8),方向:(-0.4, 0.5, -0.7)
Unity.engine.Debug:Log(Object)
TankController:Update()(在 Assets/Games/Chisana/Scripts/TankController.cs:136)
这是光线输出:
NullReferenceException:对象引用未设置为对象的实例
TankController.Update()(在 Assets/Games/Chisana/Scripts/TankController.cs:135)
如果我忽略了什么,这里是完整的脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class TankController : MonoBehaviour
{
Vector3 targetPosition;
Vector3 lookAtTarget;
public Vector4 selectedColor;
public Vector4 deselectedColor;
Quaternion playerRot;
public float rotSpeed = 2;
public float speed = 3;
bool OneExecution = false;
int count = 0;
public bool Moving = false;
public bool Selected = false;
public bool UiSelected = false;
public bool Hovering = false;
public GameObject UI;
public BoxCollider HitBox;
public DoEverything Properties;
public GameObject childObj;
public MeshRenderer mrHighlight;
public PlayerMaster playerMaster;
int playerMasterTeam;
SkinnedMeshRenderer[] skinnedMeshRenderersScan;
public List<SkinnedMeshRenderer> skinnedMeshRenderersList = new List<SkinnedMeshRenderer>();
Ray ray;
RaycastHit hit;
void Start()
{
Properties = GetComponentInChildren<DoEverything>(); //Get the DoEverything script
childObj = Properties.InstancedEntity; //Get the object it will spawn
if (mrHighlight.enabled != false && mrHighlight != null) //Make sure the highlight isn't enabled and not null
{
mrHighlight.enabled = false;
}
skinnedMeshRenderersScan = childObj.GetComponentsInChildren<SkinnedMeshRenderer>(); //Looks for all skinned mesh renderers in child object
foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
{
if (!skinnedMeshRenderersList.Contains(element)) //If it isn't already in this list
{
skinnedMeshRenderersList.Add(element); //Add to the list
}
}
playerMasterTeam = playerMaster.Team;
}
void LClickRay()
{
}
void RClickRay()
{
}
void OnMouseEnter()
{
Hovering = true;
foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
{
element.material.color = selectedColor;
}
}
void OnMouseExit()
{
Hovering = false;
foreach (SkinnedMeshRenderer element in skinnedMeshRenderersScan) //For every object it finds
{
element.material.color = deselectedColor;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.tag == "Player")
{
Physics.IgnoreCollision(collision.collider, HitBox);
}
}
void Move()
{
transform.rotation = Quaternion.Slerp(transform.rotation,
playerRot,
rotSpeed * Time.deltaTime);
transform.position = Vector3.MoveTowards(transform.position,
targetPosition,
speed * Time.deltaTime);
if (transform.position == targetPosition)
{
Moving = false;
}
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
LClickRay();
}
if (Input.GetMouseButtonDown(1))
{
RClickRay();
}
if (Moving == true)
{
Move();
}
ray = Camera.current.ScreenPointToRay(Input.mousePosition);
Debug.Log(Camera.current.ScreenPointToRay(Input.mousePosition));
}
}
【问题讨论】:
-
ray = Camera.current.ScreenPointToRay(Input.mousePosition);
-
嗯,是的,Debug 和 ray 是单独的代码行。唯一的区别是调试调用它并在控制台中显示它是什么。当 ray 调用它并将其存储在一个名为 ray 的 Ray 变量中时。
-
麻烦的是,当debug调用它时,它检测到它没有问题。当我尝试将它存储在一个变量中时,它只是假装它为空。
-
请不要将输出添加为图像,而是添加为原始文本并将其格式化为输出(前缀
>)