保存游戏对象层总是以默认方式结束(Unity + VS)

saving gameObject layer always ends up as default (Unity + VS)

所以,我一直在遵循这个指南:https://www.youtube.com/watch?v=RQyfHmf7v9s 制作一款 space 射击游戏。

想法是 "Enemy" 层下的两个对象不会发生碰撞,但是当 "Player" 层下的对象与 "Enemy" 发生碰撞时,它会成为 "invulnerable" 的一部分不与任何东西碰撞的层。

不过代码好像只有一行(这一行:)

            gameObject.layer = correctLayer;

每次都将我的对象变成 "Default" 层。 这是代码:

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
}
}

我不知道为什么启动函数没有保存正确的层,即使在统一中我已经将对象定义为它们的反射层。

注意:我已经制作了对象运动学并检查了 IsTrigger。当第一个提到的代码行未被使用时,它确实有效。

应该是void Start()不是void start()。
注意那些大写字母。