C# 保存游戏对象层始终以默认状态结束(Unity+;VS)

C# 保存游戏对象层始终以默认状态结束(Unity+;VS),c#,unity3d,visual-studio-2017,layer,C#,Unity3d,Visual Studio 2017,Layer,所以,我一直遵循这个指南: 制作一个太空射击游戏 其想法是,层“敌人”下的两个对象不会碰撞,但当层“玩家”下的对象与“敌人”碰撞时,它将成为“无敌”层的一部分,不会与任何东西碰撞 但似乎有一行代码(这一行:) 每次都将我的对象转换为“默认”层。 代码如下: using System.Collections; using System.Collections.Generic; using UnityEngine; [AddComponentMenu("WaterEffects/ForCamera"

所以,我一直遵循这个指南: 制作一个太空射击游戏

其想法是,层“敌人”下的两个对象不会碰撞,但当层“玩家”下的对象与“敌人”碰撞时,它将成为“无敌”层的一部分,不会与任何东西碰撞

但似乎有一行代码(这一行:)

每次都将我的对象转换为“默认”层。 代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("WaterEffects/ForCamera")]
public class HitHandler : MonoBehaviou {

public int health = 1; //Object health

float invulnTimer = 0.25f; //Time in which the gameObject is invulnerable
int correctLayer; // save the original layer of the object

void start()
{
    correctLayer = gameObject.layer;    
    gameObject.SetActive(true);
}

void OnTriggerEnter2D()
{
    Debug.Log("Trigger!");
    health--;
    invulnTimer = 2f;
    gameObject.layer = 10; //put object in invulnerable layer
}
// Update is called once per frame
void Update()
{
    invulnTimer -= Time.deltaTime;
    if (invulnTimer <= 0)
    {
        gameObject.layer = correctLayer; //returns object to original layer (doesnt work)
    }
    if (health <= 0)
    {
        Die();
    }
}
void Die()
{
    Destroy(gameObject); // Destroys object
}
}
使用系统集合;
使用System.Collections.Generic;
使用UnityEngine;
[添加组件菜单(“WaterEffects/ForCamera”)]
公共类HitHandler:MonoBehaviou{
public int health=1;//对象运行状况
float invulnTimer=0.25f;//游戏对象不受攻击的时间
int correctLayer;//保存对象的原始层
void start()
{
correctLayer=gameObject.layer;
gameObject.SetActive(true);
}
void OnTiggerEnter2D()
{
Log(“触发器!”);
健康--;
invulnTimer=2f;
gameObject.layer=10;//将对象放在无敌层
}
//每帧调用一次更新
无效更新()
{
invulnTimer-=Time.deltaTime;

如果(invulnTimer它应该是void Start()而不是void Start()。

请注意这些大写字母。

非常感谢,我觉得自己太笨了。不敢相信我已经在上面写了好几个小时了,因为只有一个大写字母……调试是你的朋友。如果有问题,请首先检查代码到达的位置。@joreldraw由于其VS+Unity,调试似乎不起作用(因为游戏在Unity中,而VS只是为了编写它),或者我可能缺少某些内容。您可以将VS附加到Unity并进行调试。是的,您缺少某些内容:)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[AddComponentMenu("WaterEffects/ForCamera")]
public class HitHandler : MonoBehaviou {

public int health = 1; //Object health

float invulnTimer = 0.25f; //Time in which the gameObject is invulnerable
int correctLayer; // save the original layer of the object

void start()
{
    correctLayer = gameObject.layer;    
    gameObject.SetActive(true);
}

void OnTriggerEnter2D()
{
    Debug.Log("Trigger!");
    health--;
    invulnTimer = 2f;
    gameObject.layer = 10; //put object in invulnerable layer
}
// Update is called once per frame
void Update()
{
    invulnTimer -= Time.deltaTime;
    if (invulnTimer <= 0)
    {
        gameObject.layer = correctLayer; //returns object to original layer (doesnt work)
    }
    if (health <= 0)
    {
        Die();
    }
}
void Die()
{
    Destroy(gameObject); // Destroys object
}
}