Unity公共变量在不同脚本中返回先前的值 – 如何修复?

huangapple go评论85阅读模式
英文:

Unity public variable returning previous value in separate script - how to fix?

问题

A public variable is returning its previous value when referenced in a different script in Unity.

Basically, I want to compare two variables, one of them being oppChoice. I set it to public string before this function:

void Update()
{
    if (BS.triggerOpp) //BS another script referenced before Update()
    {
        string[] rpsChoice = { "R", "P", "S" };

        System.Random random = new System.Random();
        oppChoice = rpsChoice[random.Next(0, rpsChoice.Length)];

        if (oppChoice == "R")
        {
            spriteRenderer.sprite = rockSprite;
        }
        else if (oppChoice == "P")
        {
            spriteRenderer.sprite = paperSprite;
        }
        else if (oppChoice == "S")
        {
            spriteRenderer.sprite = scissorsSprite;
        }

        anim.SetTrigger("IsRespond");

        BS.triggerOpp = false;

        Debug.Log(oppChoice);
    }
}
}

Every time BS.triggerOpp == true the variable gets assigned either "R", "P" or "S" and the sprite changes accordingly. When I use Debug.Log to print it through this script, the output always matches the sprite.

Since I want this variable to get compared with another one, I made a new script:

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

public class ScoreWhatBeatsWhat : MonoBehaviour
{
    public ButtonsScript player; //the other variable's script
    public OppRandomScript opp;  //oppChoice's script
  
    public void CompareTwoVariables()
    {
       //code comparing the two variables NOT altering opp.oppChoice in ANY way

       Debug.Log(opp.oppChoice);
    }
}

Both of these functions are triggered at the same time by the click of a button and they are both printing out the value of oppChoice, so they should be printing out the same value. When running the game, the functions are triggered more than once, therefore the variable's value changes every time. The script in which the variable is originally set (1st one) returns the correct value, however the other script, in which it's only referenced, returns its previous value.

So when I ran the game, if the first value that is randomized after trigger is "P", Debug.Log(oppsChoice) from the first script will print "P", but Debug.Log(opp.oppChoice) from the second one won't print out anything. Second time it's triggered the first script will print whatever the new value is, for example "R", but the second script will return the previous value, "P". This just keeps going every time the functions are triggered and opp.oppChoice is always printing the previous value. There are no errors and everything but that runs exactly how it's supposed to, but I can't continue with my project until the variable returns the correct value in both cases, as it is part of the core game mechanic.

I've been trying to find a solution for almost an entire day but nothing seems to work.

I tried:

  • making the oppChoice variable global

  • altering the first script so that instead of Update() and if (trigger) it was a simple public function that I attached to OnClick()

  • storing oppChoice's value in a different variable and printing that one through the second script

  • tweaking every little thing

** None of that worked. **

Every attempt returns this in the Console

I spend hours looking it up and I found that apparently something called "Events and Delegates" might help, but this is my first Unity project and, from what I read it seems way too advanced for my level.

I don't know what else to do so any input as to how to solve this issue would be really helpful Unity公共变量在不同脚本中返回先前的值 – 如何修复? Thanks!

英文:

A public variable is returning its previous value when referenced in a different script in Unity.

Basically, I want to compare two variables, one of them being oppChoice. I set it to public string before this function:

 void Update()
    {
        if (BS.triggerOpp) //BS another script referenced before Update()
        {
            string[] rpsChoice = { "R", "P", "S" };

            System.Random random = new System.Random();
            oppChoice = rpsChoice[random.Next(0, rpsChoice.Length)];

            if (oppChoice == "R")
            {
                spriteRenderer.sprite = rockSprite;

            }
            else if (oppChoice == "P")
            {
                spriteRenderer.sprite = paperSprite;

            }
            else if (oppChoice == "S")
            {
                spriteRenderer.sprite = scissorsSprite;
            }

            anim.SetTrigger("IsRespond");

            BS.triggerOpp = false;

            Debug.Log(oppChoice);
        }
    }
}

Every time BS.triggerOpp == true the variable gets assigned either "R", "P" or "S" and the sprite changes accordingly. When I use Debug.Log to print it through this script, the output always matches the sprite.

Since I want this variable to get compared with another one, I made a new script:

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

public class ScoreWhatBeatsWhat : MonoBehaviour
{
    public ButtonsScript player; //the other variable's script
    public OppRandomScript opp;  //oppChoice's script
  
    public void CompareTwoVariables()
    {
       //code comparing the two variables NOT altering opp.oppChoice in ANY way

       Debug.Log(opp.oppChoice);
    }
}

Both of these functions are triggered at the same time by the click of a button and they are both printing out the value of oppChoice, so they should be printing out the same value. When running the game, the functions are triggered more than once, therefore the variable's value changes every time. The script in which the variable is originally set (1st one) returns the correct value, however the other script, in which it's only referenced, returns it's previous value.

So when I ran the game, if the first value that is randomized after trigger is "P", Debug.Log(oppsChoice) from the first script will print "P", but Debug.Log(opp.oppChoice) from the second one won't print out anything. Second time it's triggered the first script will print whatever the new value is, for example "R", but the second script will return the previous value, "P". This just keeps going every time the functions are triggered and opp.oppChoice is always printing the previous value. There are no errors and everything but that runs exactly how it's supposed to, but I can't continue with my project until the variable returns the correct value in both cases, as it is part of the core game mechanic.

I've been trying to find a solution for almost an entire day but nothing seems to work.

I tried:

  • making the oppChoice variable global

  • altering the first script so that instead of Update() and if (trigger) it was a simple public function that I attached to OnClick()

  • storing oppChoice's value in a different variable and printing that one through the second script

  • tweaking every little thing

** None of that worked. **

Every attempt returns this in the Console

I spend hours looking it up and I found that apparently something called "Events and Delegates" might help, but this is my first Unity project and, from what I read it seems way to advanced for my level.

I don't know what else to do so any input as to how to solve this issue would be really helpful Unity公共变量在不同脚本中返回先前的值 – 如何修复? Thanks!

答案1

得分: 1

It's fixed! After a lot of trial and error, I finally looked into events and used an event to alert my second script when the oppChoice's value is changed. Had to create another script and alter a few things, but at the end of the day, it finally returns the correct value!

英文:

It's fixed! After a lot of trial and error, I finally looked into events and used an event to alert my second script when the oppChoice's value is changed. Had to create another script and alter a few things, but at the end of the day, it finally returns the correct value!

huangapple
  • 本文由 发表于 2023年6月5日 04:00:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/76402190.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定