我试图从脚本PlayerHealth.cs
获得一个浮动变量,并在脚本LightLevel
中使用它,但我在第23行得到错误信息Cannot implicitly convert type 'float' to 'UnityEngine.GameObject'
。我不知道该怎么修理它。
我还解释了我试图在LightLevel.cs
脚本的顶部做什么,以防需要。
LightLevel.cs
:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// This script is meant to get the health variable from the 'PlayerHealth.cs' script and change it to
// Intensity of the light this script is on, meaning lower the health, lower the light intensity.
// However, it's giving this error message:
// AssetsscriptsLightLevel.cs(23,18): error CS0029: Cannot implicitly convert type 'float' to 'UnityEngine.GameObject'
// How would I fix this?
public class LightLevel : MonoBehaviour
{
public GameObject player;
Light myLight;
public float LevelOfLight2;
// Start is called before the first frame update
void Start()
{
player = GameObject.Find("Player").GetComponent<PlayerHealth>().Health; // <- offending line of code
}
// Update is called once per frame
void Update()
{
Debug.Log("light level is : " + LevelOfLight2);
myLight.intensity = LevelOfLight2 + 27f;
}
}
PlayerHealth.cs
:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// everything in this script is working
public class PlayerHealth : MonoBehaviour
{
// All variables
[SerializeField] private Transform player;
[SerializeField] private LayerMask HurtLayer;
public float MaxHealth = 6f;
public float IFrame = 1f;
private float IFrameTime = 0f;
private bool AllowDamage = false;
private float ActualIFrame = 0f;
public float Health = 6f;
public float LevelOfLight = 0f; // <- The variable I want to access from "LightLevel.cs" script
// Set health to the max and do the math for I-frames
void Start()
{
Health = MaxHealth;
ActualIFrame = IFrame * 6f;
}
// Update is called once per frame
void Update()
{
// Every frame increase IFrameTime by 0.1
IFrameTime = IFrameTime += 0.1f;
// If i-frame counter is higher than teh set amount, and the player is touching hurt collider, take damage.
if (ActualIFrame < IFrameTime && hurt())
{
Debug.Log("ouch");
Health = Health - 1f;
IFrameTime = 0f;
}
// If the player's health runs out, display message "ded"
if (Health == 0f)
{
Debug.Log("ded");
}
}
// Detects if the player's collider is touching the collider of something that damages.
private bool hurt()
{
return Physics2D.IsTouchingLayers(player.GetComponent<Collider2D>(), HurtLayer);
}
}
我试着让这行代码:
player = GameObject.Find("Player").GetComponent<PlayerHealth>().Health;
:
GameObject.Find("Player").GetComponent<PlayerHealth>().Health;
但是,它只是给出了一个不同的错误信息。
错误告诉你的是你已经有了float
并且你试图将它分配给不适合保存浮点数的东西(在本例中是GameObject
)
在这一行中,你告诉Unityplayer
是一个GameObject(带有变换、位置、名称、附加脚本等的常规Unity对象)
public GameObject player;
在这一行中,你告诉unity获取生命值并用它替换当前的player
对象…
player = GameObject.Find("Player").GetComponent<PlayerHealth>().Health; // <- offending line of code
由于player
是GameObject
,所以它不能直接容纳浮点数
请注意,因为这是在Start()
方法中,它只会在启动时读取一次,然后再也不会更新。
你更可能想做这样的事情…
// At the top of the class, next to player
// No need to be public as you're setting and using it purely in code in this class
PlayerHealth playerHealth;
在你的开始方法中,你会得到一个关于玩家的生命值脚本的参考(但不是当前的生命值)…
playerHealth = GameObject.Find("Player").GetComponent<PlayerHealth>();
然后在更新方法中,可以使用playerHealth.Health
获取当前运行状况值。