简体   繁体   中英

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. 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":

public static string teamname; 

And on my "LogoDisplayer", I have this (scr_CreateTeams is a 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. When I click on Team A, if I get Team A; I also get Team A when I click on Team B or Team C. All LogoDisplayer scripts act at only one ScriptableObject. When I delete Team A from the scene, the variable returns as Team B or Team C but with the same error.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM