简体   繁体   English

我将一些数据传输到带有ScriptableObject的脚本中,但它卡在一个变量中

[英]I transfer some data to a script with a ScriptableObject but it's stuck at one variable

I made some UI Image gameobjects to show pictures via ScriptableObjects. 我制作了一些UI Image游戏对象,以通过ScriptableObjects显示图片。 And when clicked on one of them, I want it to transfer its data to an another script's static variable. 当单击其中一个时,我希望它将其数据传输到另一个脚本的静态变量。

Let's say I have this on "MyTeamProfile": 假设我在“ MyTeamProfile”上有这个:

public static string teamname; 

And on my "LogoDisplayer", I have this (scr_CreateTeams is a ScriptableObject): 在我的“ LogoDisplayer”上,我有这个(scr_CreateTeams是一个ScriptableObject):

public scr_CreateTeams thisTeam;
void Update() {
    if (Input.GetButtonDown("Fire1")) {
         MyTeamProfile.teamname = thisTeam.teamname;
    }
}

I currently has three teams with logos and a LogoDisplayer with the teams' ScriptableObjects attached to, and when I click on any of the logos I only get one team back. 我目前有三个带徽标的团队和一个与团队的ScriptableObjects相连的LogoDisplayer,当我单击任何徽标时,我只能让一个团队回来。 When I click on Team A, if I get Team A; 当我点击团队A时,如果我得到了团队A; I also get Team A when I click on Team B or Team C. All LogoDisplayer scripts act at only one ScriptableObject. 当我单击团队B或团队C时,我也得到了团队A。所有LogoDisplayer脚本仅作用于一个ScriptableObject。 When I delete Team A from the scene, the variable returns as Team B or Team C but with the same error. 当我从场景中删除团队A时,变量将返回为团队B或团队C,但存在相同的错误。

A static variable is shared across all instances of a class. 静态变量在类的所有实例之间共享。 It is not scoped to an instance of a class, but instead across all of them. 它的作用域不是类的实例,而是所有类的作用域。

If you want each "MyTeamProfile" to have a different "teamname" value, then you'll need to have a direct reference to the scriptableObject that holds that information, as well as a non-static variable holding the string. 如果希望每个“ MyTeamProfile”具有不同的“ teamname”值,则需要直接引用保存该信息的scriptableObject,以及包含该字符串的非静态变量。 This is different than your current generic class reference to the static value. 这与您当前对静态值的通用类引用不同。

Unfortunately, you do not provide enough code to provide a good example, as I cannot tell what your monobehaviours and scriptable object classes are based on what you provided. 不幸的是,您没有提供足够的代码来提供一个很好的示例,因为我无法确定您的单行为和可编写脚本的对象类是基于提供的内容。

If there's several of "LogoDisplayer" scripts on the scene, the problem is they all are just the same. 如果场景中有多个“ LogoDisplayer”脚本,则问题在于它们都是相同的。 In other words, you have several scripts doing the same things at the same time, and with the same conditions. 换句话说,您有多个脚本同时在相同条件下执行相同的操作。 When you pressing "Fire1" in your case, all of the "LogoDisplayer" scripts running MyTeamProfile.teamname = thisTeam.teamname; 在您的情况下按“ Fire1”时,所有运行MyTeamProfile.teamname = thisTeam.teamname;的“ LogoDisplayer”脚本MyTeamProfile.teamname = thisTeam.teamname; . The following code may help to understand what needs to be changed firstly. 以下代码可能有助于了解首先需要更改的内容。 Try it: 试试吧:

public KeyCode keyCode; //Select DIFFERENT KeyCodes for each team in the Inspector
                        //For example, Team A = KeyCode.A, Team B = KeyCode.B, etc
    public scr_CreateTeams thisTeam;

    void Update()
    {
        if (Input.GetButtonDown(keyCode))
        {
            MyTeamProfile.teamname = thisTeam.teamname;
        }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 ScriptableObject 中的脚本? - How do I use a script that is in my ScriptableObject? 在 Unity 中的 ScriptableObject 中序列化脚本实例 - Serialize Script Instance in a ScriptableObject in Unity 统一。 如何在 ScriptableObject 的代码中存储通用数据以供使用(不同的 NPC 类型) - Unity. How to store generic data for usage in ScriptableObject's code (different NPC types) 将从scriptableobject继承的脚本添加到游戏对象 - Add script inheriting from scriptableobject to game object 重新加载程序集以访问新的ScriptableObject脚本类型 - Reload Assembly for access to new ScriptableObject script type ScriptableObject 进入播放模式时不保存数据 - ScriptableObject not saving data when entering play mode 控制台和表格合二为一和数据传输? - Console and form in one and data transfer? 如何将html表数据以可以保存在sql数据库中的方式传输到Jquery脚本? - How can I transfer html table data to Jquery script in a way that I can save it in the sql database? 想在一个变量中添加多个变量的数据,数组/列表 - Want to add multiple variable's data in one variable, array/list 统一错误(脚本检查器3):不允许从ScriptableObject构造函数调用GetBool - Error in unity(Script Inspector 3): GetBool is not allowed to be called from a ScriptableObject constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM