YueZHeng_917 2022-07-14 23:23 采纳率: 61.1%
浏览 95
已结题

Unity不同脚本同样的public如何同步更改

问题遇到的现象和发生背景

在脚本TankMove和TankAttack中的第七行,都有一个一样的东西(不知道应该叫什么),number。无论在脚本中还是在Unity检查器中,这两个number想要修改就必须两个数值一个个改过来,如果是3个脚本,4个脚本,这种方法就会很麻烦。可不可以让我修改其中一个脚本的number就能使所有使用number的脚本,进行同步更新修改呢?

问题相关代码,请勿粘贴截图
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TankMove : MonoBehaviour
{
    public int number = 1;//设立序号number(我是第七行!!!!!!!!!!!!!!!)
    public float moveSpeed = 5;//设立数值移动速度为5
    public float rotateSpeed = 30;//设立数值旋转速度为30
    public float wsy;//设立数值wsy
    public float ady;//设立数值ady
    public Rigidbody GangTi;//设立刚体对象gangti

    void Start()
    {

    }

    void Update()
    {
        wsy = Input.GetAxis("WSYP" + number);//使wsy和输入管理器中的轴有关联
        ady = Input.GetAxis("ADYP" + number);//使ady和输入管理器中的轴有关联

        if (wsy != 0)//如果wsy的值不为0,则向前或向后移动,方向取决于数值的正负
        {
            Rigidbody GangTi = this.gameObject.GetComponent<Rigidbody>();//
            if (GangTi != null)//
            {
                Vector3 targetPos = GangTi.position + GangTi.transform.forward * wsy * moveSpeed * Time.deltaTime;//
                GangTi.MovePosition(targetPos);//
            }
        }

        if (ady != 0)//如果ady的值不为0,则向左或向右旋转,方向取决于数值的正负
        {
            if (wsy < 0)//此代码解决按下SD(右后)实际往左后移动的bug
            {
                ady = -ady;//
            }
            Vector3 rotateValue = Vector3.up * ady * rotateSpeed * Time.deltaTime;//
            this.transform.Rotate(rotateValue);//
        }
    }
}


分割线

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

public class TankAttack : MonoBehaviour
{
    public float number = 1;//设立序号numbr
    public GameObject ShellPrefab;//设立游戏对象子弹预制体
    public string FireKey = "FireP";//设立按键FireKey
    public float shellSpeed = 100;//子弹速度为100
    private Transform firePoint1;//发射位置FirePoint1
    public bool Fire1 = true;//子弹发射许可设置为true
    public float jiangetime = 3.0f;//攻击间隔jiangetime设置为3.0秒
    public float deltatime = 0;//时间累加
    void Start()
    {
        firePoint1 = transform.Find("FirePoint1");//自动寻找发射位置FirePoint1
        FireKey = "FireP" + number;//(在Unity的输入管理器中FireP1的肯定按钮为space)
    }
    void Update()
    {
        if (Input.GetButtonDown(FireKey))//如果按下了FireKey,执行下方内容
        {
            if (Fire1)
            {
                Fire1 = false;//攻击后进入间隔,子弹发射许可设置为false
                GameObject go = GameObject.Instantiate(ShellPrefab, firePoint1.position, firePoint1.rotation) as GameObject;//在发射点位置实例游戏对象ShellPrefab。
                go.GetComponent<Rigidbody>().velocity = go.transform.forward * shellSpeed;//给予子弹初速度
            }
        }
        else if (!Fire1)
        {
            deltatime += Time.deltaTime;//deltatime随时间增加而增加
            if (deltatime > jiangetime)//如果deltatime大于jiangetime(3秒),执行下方内容
            {
                Fire1 = true;//子弹发射许可设置为ture
                deltatime = 0;//deltatime归零
            }
        }
    }
    
}


运行结果及报错内容
我的解答思路和尝试过的方法
我想要达到的结果
  • 写回答

2条回答 默认 最新

  • 周周的Unity小屋 2022-07-15 00:33
    关注

    1.既然你个number变量时多个脚本同时用到的,你让其中一个脚本作为单例脚本,单例脚本声明这个number变量,其他脚本调用就行呀,不用每个脚本都声明一个number变量。
    2.或者你写一个全局静态变量管理类,声明静态变量numbe,提供给其他脚本使用就行

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 7月29日
  • 已采纳回答 7月21日
  • 创建了问题 7月14日

悬赏问题

  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站