简体   繁体   中英

Can't Assign Struct Equal to Struct from another Script

Im working on player movement coding, and I want to have premade movement stats prepared for "Power ups" and so forth. I made a stats management script separate from my movement script for the sake of organization and I have the movement variables packed in structs so that they can be switched out during Play Mode.

I can import the Outgoing Stats struct from the stats management script, but I want to assign the struct to its own variable in the movement script so that its easier to handle. It does copy but my new struct variable returns 0 on all of its internal variables instead of copying the values from the original struct.

Tl;dr Im trying to copy a struct from one script to the other but when said struct copies, the copy has variables with 0.

Stats Management Script

public struct StatsSet
{
    public int JumpCount;
    public float JumpPower;
    public float MoveSpeed;
    public float QuickFallMultiplier;
    public float BaseFallMultiplier;
}

//Public variables for experimental movement stats testing
public int ExJumpCount = 1;
public float ExJumpPower = 6.0f;
public float ExMoveSpeed = 4.0f;
public float ExQuickFallMultiplier = 2.0f;
public float ExBaseFallMultiplier = 2.5f;

public int Statpick = 1;
public StatsSet StatsOut;
private StatsSet ExStats;
private StatsSet BaseStats;

// Use this for initialization
void Start () {
    BaseStats.JumpCount = 1;
    BaseStats.JumpPower = 6.0f;
    BaseStats.MoveSpeed = 4.0f;
    BaseStats.QuickFallMultiplier = 2.0f;
    BaseStats.BaseFallMultiplier = 2.5f;

    ExStats.BaseFallMultiplier = ExBaseFallMultiplier;
    ExStats.JumpCount = ExJumpCount;
    ExStats.JumpPower = ExJumpPower;
    ExStats.MoveSpeed = ExMoveSpeed;
    ExStats.QuickFallMultiplier = ExQuickFallMultiplier;
    Statpick = 1;
}

// Update is called once per frame
void Update () {
    if (Statpick == 0)
    {
        StatsOut = ExStats;
    }
    else if (Statpick == 1)
    {
        StatsOut = BaseStats;
    }
}

Movement Script

private Rigidbody PlayerRigid;
private int JumpCheck;
private bool IsGrounded = true;
public Collider GroundCollider;
private int HiddenJumpCount;

//Import of player stats data
private PlayerStats PStats;
private PlayerStats.StatsSet PlayerStats;

// Use this for initialization
void Start()
{
    PStats = GetComponent<PlayerStats>();
    PlayerRigid = GetComponent<Rigidbody>();
    PlayerStats = PStats.StatsOut;
    JumpCheck = PlayerStats.JumpCount;
    Debug.Log("Up and running...");
}

PStats.Out returns the values Im expecting, but PlayerStats returns what seems to be null values

These scripts are both using Start() so my guess is that MovementScript.Start() is happening first and when it grabs the struct, it's grabbing a default(StatsSet) because your Stats Management Script hasn't initialized it yet. You can throw a Debug.Log() into each to confirm that's your issue but here's how to fix it:

One option is to rename Start() to Awake() in your Stats Management Script. You can learn more about Awake vs Start in this video but TL;DR, all active scripts run their Awake() then all scripts run their Start() .

Your other option is to change the "Script Execution Order". If you don't touch the Script Execution Order Settings , the order of different Start() functions will be unknown. You can use that settings menu to instead ensure your Stats Management Script always executes before your Movement Script by giving it an explicit execution order.

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