简体   繁体   中英

How do I access an updated variable between two scripts in Unity with C#?

Hopefully this isn't too much detail, I'm not used to asking programming questions.

I'm attempting to do the 3D Video Game Development with Unity 3D course that's on Udemy, though using C# instead of Javascript. I just finished up the tutorial that involves creating a space shooter game.

In it, a shield is created by the user when pressing a button. The shield has a "number of uses" variable that does not actually get used by the time the tutorial has finished. I'm trying to add it in, and have successfully managed to implement it so that with each use, we decrease the number of uses remaining, and no longer are able to instantiate the shield once that number is <=0.

This variable is stored on the player, and if I print it from the player, it returns the value I would expect.

However, I'm using a separate SceneManager.cs (this is where the tutorial placed the lives, and score, and timer variables ) where I print numbers into the GUI. My problem is that I cannot get my number of uses variable to stay current when I try to print it from the scene manager... it registers the initial value, but doesn't update after that.

Here is the Player Script

using UnityEngine;
using System.Collections;

public class player_script : MonoBehaviour {

    // Inspector Variables
    public int numberOfShields  = 2;        // The number of times the user can create a shield
    public Transform shieldMesh;                // path to the shield
    public KeyCode shieldKeyInput;              // the key to activate the shield
    public static bool shieldOff    = true;     // initialize the shield to an "off" state

    public int NumberOfShields
    {
        get{return numberOfShields;}
        set{numberOfShields = value;}
    }

    // Update is called once per frame
    void Update()
    {
        // create a shield when shieldKey has been pressed by player
        if (Input.GetKeyDown (shieldKeyInput)) {
            if(shieldOff && numberOfShields>0)
            {
                // creates an instance of the shield
                Transform clone;
                clone = Instantiate (shieldMesh, transform.position, transform.rotation) as Transform;

                // transforms the instance of the shield
                clone.transform.parent = gameObject.transform;

                // set the shield to an on position
                shieldOff = false;

                // reduce the numberOfShields left
                numberOfShields -=1;

            }
        }

        print ("NumberOfShields = " + NumberOfShields);
    }

    public void turnShieldOff()
    {
        shieldOff = true;
    }

}

when I run "print ("NumberOfShields = " + NumberOfShields);" I get the value I expect. (astroids trigger the turnShieldOff() when they collide with a shield.

Over in my Scene Manager however... this is the code I'm running:

using UnityEngine;
using System.Collections;

public class SceneManager_script : MonoBehaviour {

// Inspector Variables
public GameObject playerCharacter;
private player_script player_Script;
private int shields = 0;

// Use this for initialization
void Start ()
{
    player_Script = playerCharacter.GetComponent<player_script>();
}

// Update is called once per frame
void Update ()
{
    shields = player_Script.NumberOfShields;
    print(shields);
}

// GUI
void OnGUI()
{
    GUI.Label (new Rect (10, 40, 100, 20), "Shields: " + shields);
}
}

Any idea what I'm doing wrong that prevents shields in my SceneManager script from updating when NumberOfShields in my player_script updates?

I think you might have assigned a prefab into playerCharacter GameObject variable instead of an actual in game unit. In this case it will always print the default shield value of prefab. Instead of assigning that variable via inspector try to find player GameObject in Start function. You can for example give your player object a tag and then:

void Start() {
    playerCharacter = GameObject.FindGameObjectWithTag("Player");
}

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