【问题标题】:Why when starting the game the script is turned off ? I need to turn it on after the game is running为什么开始游戏时脚本被关闭?我需要在游戏运行后打开它
【发布时间】:2019-04-18 14:45:15
【问题描述】:

我有一个预制名称子弹。 我把预制件拖到武器下面。 脚本已打开,但在启动游戏时脚本已关闭,我需要再次打开它。

在屏幕截图中,子弹预制件位于中间。在层次结构的左侧,项目符号位于 SciFiRifle(Clone) 的下方,在底部的 Inspector 中右侧有一个脚本附加名称为 Shooting。

脚本已开启。

但是当我运行游戏时,脚本由于某种原因被关闭: 首先出于某种原因,我还需要折叠 SciFiRifle(Clone) 以在层次结构中再次看到子弹:

并且脚本已关闭:

我不明白为什么它会在运行游戏时将项目符号隐藏在层次结构中并关闭脚本。

脚本:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Shooting : MonoBehaviour
{
    public GameObject projectile;
    public Animator anim;

    private void Start()
    {
        anim.SetBool("Shooting", true);
    }

    private void Update()
    {
        if (anim.GetBool("Shooting") == true)
        {
            GameObject bullet = Instantiate(projectile, transform.position, Quaternion.identity) as GameObject;
            bullet.GetComponent<Rigidbody>().AddForce(transform.forward * 10, ForceMode.VelocityChange);
        }
    }
}

更新:

我找到了问题所在。还没怎么解决。 对象层次结构中的第二个孩子是 Sci-Fi_Soldier。

Sci-Fi_Soldier 附加了一些脚本,其中之一是 Player Controller,它不是我的脚本。

Inspector 中此脚本的一些属性获取 Right Gun 我分配给 Right Fun 的是 SciFiRifle(Clone),而 Bullet 是 SciFiRifle(Clone) 的子项。

你可以看到它标记为黄色:

如果我在所有 Player Controller 中删除此脚本,那么在 Bullet 上射击脚本将保持打开状态并保持折叠状态。

我无法弄清楚播放器控制器脚本中的问题所在:

using UnityEngine;
using System.Collections;

[RequireComponent (typeof (Animator))]
public class PlayerController : MonoBehaviour {

    public Transform rightGunBone;
    public Transform leftGunBone;
    public Arsenal[] arsenal;

    private Animator animator;

    void Awake() {
        animator = GetComponent<Animator> ();
        if (arsenal.Length > 0)
            SetArsenal (arsenal[0].name);
        }

    public void SetArsenal(string name) {
        foreach (Arsenal hand in arsenal) {
            if (hand.name == name) {
                if (rightGunBone.childCount > 0)
                    Destroy(rightGunBone.GetChild(0).gameObject);
                if (leftGunBone.childCount > 0)
                    Destroy(leftGunBone.GetChild(0).gameObject);
                if (hand.rightGun != null) {
                    GameObject newRightGun = (GameObject) Instantiate(hand.rightGun);
                    newRightGun.transform.parent = rightGunBone;
                    newRightGun.transform.localPosition = Vector3.zero;
                    newRightGun.transform.localRotation = Quaternion.Euler(90, 0, 0);
                    }
                if (hand.leftGun != null) {
                    GameObject newLeftGun = (GameObject) Instantiate(hand.leftGun);
                    newLeftGun.transform.parent = leftGunBone;
                    newLeftGun.transform.localPosition = Vector3.zero;
                    newLeftGun.transform.localRotation = Quaternion.Euler(90, 0, 0);
                }
                animator.runtimeAnimatorController = hand.controller;
                return;
                }
        }
    }

    [System.Serializable]
    public struct Arsenal {
        public string name;
        public GameObject rightGun;
        public GameObject leftGun;
        public RuntimeAnimatorController controller;
    }
}

它将子弹隐藏在层次结构中,并关闭脚本射击。但是脚本 Shooting 不会在层次结构中的其他任何地方或任何其他脚本中使用。但是通过尝试删除脚本,我发现这个脚本是使 Bullet 出现问题的脚本。

这是 SciFiRifle(Clone) 的 Inspector 截图:

【问题讨论】:

  • 可能是因为其他脚本将其关闭...尝试禁用其他脚本,直到找到罪魁祸首
  • 尝试通过GetComponent获取脚本并调用script.SetActive(true)
  • 我找到了问题所在,但我仍然无法弄清楚问题是什么或为什么会发生。我将更新我在问题中发现的内容。
  • 最重要的是:子弹应该作为枪的子级生成!如果你这样做,所有发射的子弹都将与玩家或枪支对象一起平移/缩放/旋转,这样你就可以在射击后“控制”它们的方式 ^^ 子弹应该被生成到一个从未移动过的物体中,这样它们就可以跟随它们笔直而独立的方式

标签: c# unity3d


【解决方案1】:

PlayerController 脚本实际上会销毁您的 Sci-fi Rifle(您放入预制件中的那个),然后使用 PlayerController 脚本的 Right Gun 字段中提供的那个实例化一个新的步枪(这就是您需要再次折叠它,因为您看到的是步枪预制件的新实例)。

PlayerController 中描述此行为的行是:

                if (rightGunBone.childCount > 0)
                    Destroy(rightGunBone.GetChild(0).gameObject);

这会破坏您放入预制件中的步枪。并且:

                if (hand.rightGun != null) {
                    GameObject newRightGun = (GameObject) Instantiate(hand.rightGun);
                    newRightGun.transform.parent = rightGunBone;

这会生成您在 Right Gun 字段中提供的预制件的新实例,替换您放入预制件中的步枪。

您可以尝试:从 PlayerController 字段中删除预制件,或者从预制件中删除 Sci-fi Rifle。

【讨论】:

    猜你喜欢
    • 2023-03-23
    • 1970-01-01
    • 2021-07-20
    • 2016-09-28
    • 2011-09-25
    • 1970-01-01
    • 1970-01-01
    • 2018-05-25
    • 2015-10-20
    相关资源
    最近更新 更多