下面是代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class start效果 : MonoBehaviour
{
private Vector4 yanse;
void Update()
{
if (Input.GetMouseButton(0))
{
Ray shubiaoweizhi = new Ray();
shubiaoweizhi = Camera.main.ScreenPointToRay(Input.mousePosition);//获取鼠标在屏幕上的位置
RaycastHit start_button;//新建变量被射线碰撞到的物体
bool shubiaoshifoudiandao = Physics.Raycast(shubiaoweizhi, out start_button);//获取该射线碰撞到的物体并且赋值给一个储存器
Renderer renderer = GetComponent<Renderer>();
Material material = renderer.material;
material . color = yanse;
if (shubiaoshifoudiandao == true)
{
Debug.Log("点了");
if (start_button . collider . gameObject . name == "start button")
{
Debug.Log("点到了按钮");
Color gaibianyanse = new Color(200f, 200f, 200f, 255f);
material . color = gaibianyanse;
}
}
else
{
Debug.Log("没点到");
Color yvanlaiyanse = new Color(255f, 255f, 255f, 255f);
material . color = yvanlaiyanse;
}
if(Input.GetMouseButtonUp(0))
{
if (shubiaoshifoudiandao == true)
{
gameObject.SetActive(false);
}
}
}
}
void OnDisable()
{
}
}
控制台报的是这个错:
NullReferenceException: Object reference not set to an instance of an object start效果.Update () (at Assets/C#脚本/start效果.cs:17)
我认为报错位置是这一行代码:
shubiaoweizhi = Camera.main.ScreenPointToRay(Input.mousePosition);//获取鼠标在屏幕上的位置
就是不知道错在哪里,有哪位大佬能帮我解惑吗?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class start效果 : MonoBehaviour
{
private Vector4 yanse;
void Update()
{
if (Input.GetMouseButton(0))
{
Ray shubiaoweizhi = new Ray();
shubiaoweizhi = Camera.main.ScreenPointToRay(Input.mousePosition);//获取鼠标在屏幕上的位置
RaycastHit start_button;//新建变量被射线碰撞到的物体
bool shubiaoshifoudiandao = Physics.Raycast(shubiaoweizhi, out start_button);//获取该射线碰撞到的物体并且赋值给一个储存器
Renderer renderer = GetComponent<Renderer>();
Material material = renderer.material;
material . color = yanse;
if (shubiaoshifoudiandao == true)
{
Debug.Log("点了");
if (start_button . collider . gameObject . name == "start button")
{
Debug.Log("点到了按钮");
Color gaibianyanse = new Color(200f, 200f, 200f, 255f);
material . color = gaibianyanse;
}
}
else
{
Debug.Log("没点到");
Color yvanlaiyanse = new Color(255f, 255f, 255f, 255f);
material . color = yvanlaiyanse;
}
if(Input.GetMouseButtonUp(0))
{
if (shubiaoshifoudiandao == true)
{
gameObject.SetActive(false);
}
}
}
}
void OnDisable()
{
}
}
控制台报的是这个错:
NullReferenceException: Object reference not set to an instance of an object start效果.Update () (at Assets/C#脚本/start效果.cs:17)
我认为报错位置是这一行代码:
shubiaoweizhi = Camera.main.ScreenPointToRay(Input.mousePosition);//获取鼠标在屏幕上的位置
就是不知道错在哪里,有哪位大佬能帮我解惑吗?