简体   繁体   English

如何将类型“int”转换为“unityengine.gameobject”?

[英]How to convert type 'int' to 'unityengine.gameobject'?

I'm trying to write a script that controls respawn system of the checkpoint.我正在尝试编写一个控制检查点重生系统的脚本。 In PlayerController script, I declared "health" as public int.在 PlayerController 脚本中,我将“健康”声明为 public int。 Then, I refered "health" from it in this script.然后,我在这个脚本中引用了“健康”。 So, if player dies (health = 0), it will respawn at the checkpoint.因此,如果玩家死亡(健康 = 0),它将在检查点重生。

However, when I refer 'health' in this script, I have to convert 'int' to 'unityengine.gameobject'.但是,当我在这个脚本中提到“健康”时,我必须将“int”转换为“unityengine.gameobject”。 Can anyone help me please.谁能帮助我。 I just started to learn C#.我刚开始学习C#。

public class PlayerPros : MonoBehaviour
{
    private savePoint sp;
    private GameObject health;


    private void Start()
    {
        health = GetComponent<GameObject>();
        sp = GameObject.FindGameObjectWithTag("SP").GetComponent<savePoint>();
        transform.position = sp.lastCheckPointPos;
    }

   private void Update()
    {

       if ( health = 0) //error occurs here
       {
           SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
       }
    }
}

You don't convert the type, you get the variable form the class.您不转换类型,而是从 class 中获得变量。

If the 2 scripts are on the same Gameobject you can use如果 2 个脚本在同一个 Gameobject 上,您可以使用

int health = GetComponent<PlayerController >().health;

make sure that your health variable in the PlayerController script is public确保PlayerController脚本中的健康变量是公开的

Also you can't do if ( health = 0) the correct way is if ( health == 0) if ( health = 0)你也不能做正确的方法是if ( health == 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将类型“UnityEngine.Transform”转换为“UnityEngine.GameObject” - Cannot convert type `UnityEngine.Transform' to `UnityEngine.GameObject' 无法将类型“UnityEngine.GameObject”隐式转换为“GameObject” - Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameObject' 无法将类型&#39;Vuforia.Anchor&#39;隐式转换为&#39;UnityEngine.GameObject&#39; - Cannot implicitly convert type `Vuforia.Anchor' to 'UnityEngine.GameObject' 无法将类型“UnityEngine.GameObject”隐式转换为“GameMaster”? - Cannot implicitly convert type 'UnityEngine.GameObject' to 'GameMaster'? SerializationException:类型UnityEngine.GameObject未标记为Serializable - SerializationException: Type UnityEngine.GameObject is not marked as Serializable 无法转换 Type 'UnityEngine.Rigidbody' en 'UnityEngine.GameObject 并且没有 Addforce 的定义 - Can't convert Type 'UnityEngine.Rigidbody' en 'UnityEngine.GameObject and no definition for Addforce foreach语句无法遍历UnityEngine.GameObject类型的变量 - foreach statement cannot ooperate on variables of type UnityEngine.GameObject 错误CS0021:无法将带有[]的索引应用于类型为&#39;UnityEngine.GameObject&#39;的表达式 - Error CS0021: Cannot apply indexing with [] to an expression of type `UnityEngine.GameObject' UnityEngine.GameObject与Generic.List <UnityEngine.GameObject> - UnityEngine.GameObject vs. Generic.List<UnityEngine.GameObject> “UnityEngine.GameObject[]”不包含“Transform”错误的定义 - 'UnityEngine.GameObject[]' does not contain a definition for `Transform' error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM