Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
82 views
in Technique[技术] by (71.8m points)

c# - Why is my bool value, set within a ScriptableObject, not being updated in another script?

I have a problem with my bool value that I dont understand. I have an Actions script, which derives from MonoBehavior. In that script I have a bool variable "IsCompleted", which shall indicate whether an Attack is finished or not. In Awake there I set this bool to true, to indicate that no attack is running. In another Script - ScriptableAttack - which derives from ScriptableObject, I have a function that is the main part of an attack. There I edit this value and set it to false. Then, if conditions are good, I want to run a timer after that the bool is set to false again. Now, since I can't use the typical Update method on a ScriptableObject, I want to check if this value is false in a third script I have - CharacterSettings - which derives from MonoBehavior. The problem now is that when I check that value on Update in the CharacterSettings script, it always returns true, while I see that it is indeed false within ScriptableAttack. I also checked if I overwrite this value somewhere but couldn't find anyhing. The usages I wrote down here are the only ones I have in my application. Maybe somebody can help or at least explain this behavior to me.

Here's the base of the ScriptableObjects function:

        public void Attack()
        {
            if (!_actions.IsCompleted)
            {
                return;
            }

            _actions.IsCompleted = false;

            if (Vector3.Distance(GeneralGameManager.Characters[0].transform.position,
                    GeneralGameManager.Characters[1].transform.position) > _attackDistance)
            {
                _actions.IsCompleted = true;
                return;
            }
        }

Further description: Actions is a script which basically holds the SOs and the IsCompleted bool. It looks like this:

        [SerializeField] private ScriptableAttack _frontPunch;
        [SerializeField] private ScriptableAttack _backPunch;
        [SerializeField] private ScriptableAttack _frontKick;
        [SerializeField] private ScriptableAttack _backKick;
        [HideInInspector] public ScriptableAttack[] AttackCollection;
        [HideInInspector] public bool IsCompleted;

        private void Awake()
        {
            IsCompleted = true;

            AttackCollection = new[] {FrontPunch, BackPunch, FrontKick, BackKick};
        }

The CharacterSettings Script is a script that each fighter (there are two of them) has attached. There I would like to do something like this:

        private void Update()
        {
            if (_actions.IsCompleted)
            {
                return;
            }

            timer += Time.deltaTime;

            if (timer >= attackExecutionTimer)
            {
                //Do damage and stuff
                _actions.IsCompleted = true;
                timer = 0;
            }
        }

Where timer is a float value at 0 and attackExecutionTimer is a float parameter set in the SOs.

Attack() from the SO is called on Input.GetKeyDown.

question from:https://stackoverflow.com/questions/65829846/why-is-my-bool-value-set-within-a-scriptableobject-not-being-updated-in-anothe

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...