Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/unity3d/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 更改其他脚本的值,而无需将目标脚本附加到游戏对象_C#_Unity3d - Fatal编程技术网

C# 更改其他脚本的值,而无需将目标脚本附加到游戏对象

C# 更改其他脚本的值,而无需将目标脚本附加到游戏对象,c#,unity3d,C#,Unity3d,我是C#and Unity的新手,目前在我的游戏项目中面临以下问题。 我有一个类可以为不同的商店实例化,每个商店都有不同的利润,有不同的名称,拥有不同的权力资源。因此,我的想法是使用一个类来创建这些车间,并在构造函数调用时(例如车间创建),它应该更改另一个脚本的变量,其中计算了平衡、功率等。示例代码: using System.Collections; using System.Collections.Generic; using UnityEngine; [System.Serializab

我是C#and Unity的新手,目前在我的游戏项目中面临以下问题。 我有一个类可以为不同的商店实例化,每个商店都有不同的利润,有不同的名称,拥有不同的权力资源。因此,我的想法是使用一个类来创建这些车间,并在构造函数调用时(例如车间创建),它应该更改另一个脚本的变量,其中计算了平衡、功率等。示例代码:

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

[System.Serializable]
public class Shops
{
    public int shopID;
    public string shopName;
    public float shopPower;
    public double shopCoast;
    public string shopType;
    public double shopProfit;

    public Shops(int id, string name, float power, double coast, string type, double profit)
    {
        this.shopID = id;
        this.shopName = name;
        this.shopPower = power;
        this.shopCoast = coast;
        this.shopType = type;
        this.shopProfit = profit;

        Debug.Log("Shop created");

        addProfit(profit);
        subPower(power);
        subCoast(coast);
        
    }

    

    public void addProfit(double profit)
    {
        
    }



    public void subPower(float power)
    {

    }

    public void subCoast(double coast)
    {

    }




}



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

public class Calculations : MonoBehaviour
{
    public double money;
    public float power;
    public double profitPerHour, coastPerHour, totalPerHour;




    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}
这是正确的还是说我写它的方式很普通,构造函数调用了一些方法?我如何从计算中获取变量double money:Monobeheivor脚本,而不将脚本附加到游戏对象?有没有办法不将其附加到游戏对象上?我的想法是,计算脚本完成所有需要的计算,我只是从这个脚本中获取我的UI值。或者update()和start函数不起作用吗

还有一个问题 我如何将所有创建的类对象存储在一个列表或数组中,以便以后可以将其打印到一个表中,在该表中对其进行排序,例如从最低利润到最高利润。使用数组或列表更好的方法是什么


我希望我能把我的问题说清楚,并提前向您表示感谢。

就像@Ben Rubin在他的评论中所说的,除了在构造函数中初始化变量之外,再做其他事情不是一个好主意。但是您可以执行一个方法,例如,
Init()
,它将调用其他类上的方法

如果每个商店都有一个计算实例,那么可能应该将这两个类合并到一个商店中

但是,如果您的计算是针对所有商店的全局计算,则可以将其设置为单例,而不必将其设置为单行为。在里面,你在一个列表中注册你的商店,然后你可以在计算类中访问它

公共类商店
{
...
公共商店(……)
{
//只初始化变量
}
公共void Init()
{
Calculations.Instance.RegisterShop(this,true);
//在这里打电话给其他班级
}
公共空间清除()
{
Calculations.Instance.RegisterShop(this,false);
}
...
}
公共类计算
{
私有静态计算m_实例=null;
公共静态计算实例{//singleton
得到{
if(m_实例==null)
m_实例=新计算();
返回m_实例;
}
}
私有列表shopList=新列表();
公共登记处(商店、布勒登记处)
{
bool isRegistered=shopList.Contains(shop);
如果(已注册&&!isRegistered)
商店列表。添加(商店);
如果(!register&&isRegistered),则为else
商店清单。删除(商店);
}
...
}
例如,在一个脚本
中,商店卖家
,附加到场景中的一个游戏对象上(我不知道你是否会这样做)

公共类卖主:MonoBehavior
{
私人店铺=空;
公开作废开始()
{
店铺=新店铺(…);
shop.Init();
}
公共空间
{
商店?.Clear();
}
...
}
注意需要明智地使用singleton,它并不是解决所有困难的方法。这是我能想到的唯一一种方法,让你不要做出单一行为,并将其附加到游戏对象上使用它

对于您的奖金问题,如果您想为您想要做的事情选择一个合适的容器,您可以参考

它使用C++容器类型,但对C语言的翻译保持不变:

  • 向量=列表
  • 列表=链接列表
  • 地图=字典
  • set=HashSet
  • deque=在C中没有等效物#

它唯一缺少的是基本数组,当您确切知道长度时,它很有用,并且您知道它在运行时不会增长或收缩。

1)让构造函数调用方法不是常见的做法,也不是一个好主意。原因是构造函数应该是轻量级的,因此它们通常只为一些字段和属性赋值。您不希望构造函数调用任何可能由于无效参数或空引用或其他原因引发异常的方法。不允许构造函数可能引发异常的情况。2)
monobhavior
s必须连接到游戏对象。如果它不会附加到游戏对象上,那么它就不应该是一个
单一行为
。谢谢您的回复。好的,你会这样做吗:ShopShop1=新商店(…);然后运行在Shop类中创建的方法,如shop1.addProfit(…)?然后,每次创建店铺后,我都会调用许多方法。