简体   繁体   中英

Unity Inspector clears variable on play

So I've got a really basic combat script up that takes into the inspector the player and whichever enemy the script is attached to. However, when I hit play to test the game, the value that I added to the player variable in the inspector disappears as if I never added anything. It shows up again when I hit stop.

My super simple combat script

using UnityEngine;
using System.Collections;
using UnityEditor;
using UnityEngine.UI;

public class BasicCombat : MonoBehaviour

{

public KennedyClass playerK;
public pawnClass pawn;
public archerClass archer;



// Use this for initialization
void Start()
{

    playerK = GetComponent<KennedyClass>();
    pawn = GetComponent<pawnClass>();
    archer = GetComponent<archerClass>();
    playerK.charHP = playerK.maxHP;

}

// Update is called once per frame
void Update()
{

}

public void Attack()
{

    if (gameObject.CompareTag("Pawn")) 
    {
        while (playerK.charHP != 0 & pawn.pawnHP !=0)
        {               
                playerK.charHP -= pawn.pawnDamage;
                playerK.HPCount();
                Debug.Log("Player - " + playerK.charHP);
                var option = EditorUtility.DisplayDialogComplex(
                     "Attack",
                     "Do you want to attack?",
                     "Yes",
                     "No",
                     "Exit");

                switch (option)
                {
                    case 0:
                        pawn.pawnHP -= playerK.atkP;
                        Debug.Log("Pawn - " + pawn.pawnHP);
                        break;                          
                    case 1:
                        //Exit loop (for now)
                        break;
                    case 2:
                        //Exit loop (for now)
                        break;
                }                
        }          
    }
    else if (gameObject.CompareTag("Archer"))
    {
        while (playerK.HP != 0 & pawn.pawnHP != 0)
        {
            playerK.HP -= archer.archerDamage;
            Debug.Log("Player - " + playerK.HP);
            var option = EditorUtility.DisplayDialogComplex(
                 "Attack",
                 "Do you want to attack?",
                 "Yes",
                 "No",
                 "Exit");

            switch (option)
            {
                case 0:
                    archer.archerHP -= playerK.atkP;
                    Debug.Log("Archer - " + archer.archerHP);
                    break;
                case 1:
                    //Exit loop (for now)
                    break;
                case 2:
                    //Exit loop (for now)
                    break;
            }
        }
    }


    if (playerK.charHP == 0)
    {
        Debug.Log("Player death");
        Application.LoadLevel(0);
    }


    if (pawn.pawnHP == 0)
    {
        Debug.Log("Enemy death");
        pawn.GetComponent<Renderer>().enabled = false;
    }
    if (archer.archerHP == 0)
    {
        Debug.Log("Enemy death");
        archer.GetComponent<Renderer>().enabled = false;
    }
  }
}

You are changing the variable in the Start funciton. Remove the line

playerK = GetComponent<KennedyClass>();

from your Start function to leave the variable as it is set in the inspector.

To explain what's going on a little, when you assign the player variable in the inspector you are giving it a reference to a GameObject with a KenedyClass script attached to it. This is the reference that I am assuming you want.

However , when you call playerK = GetComponent<KennedyClass>(); you are then saying 'forget what I set in the inspector, look for a KennedyClass component on the game object that this script is attached to and set playerK equal to it' . If there is not a KennedyClass script attached to this gameObject, which I am assuming there isn't, then it will return null and wipe the settings in the inspector.

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