Ok, I’ve been fighting this all day long. Maybe someone can help me figure out what’s up with my script. I’ve read other threads on this all day, and tried all the different ways I’ve seen it done. None of them seem to work… all I need to do is read a Boolean variable from Script A as one of my conditions for an if Statement in Script B. However, right now I’m simply trying to get it to print out the value. Currently I have the value set to TRUE, but when Script B prints it out, it prints as false. Can you tell me why?
Script A:
using UnityEngine;
using System.Collections;
public class PlayerController : MonoBehaviour
{
public bool hidden = true;
// Use this for initialization
void Start ()
{
}
}
Then Script B:
using UnityEngine;
using System.Collections;
public class EnemyController : MonoBehaviour
{
public PlayerController playerController;
// Use this for initialization
void Start ()
{
Debug.Log ("Am I hidden? " + playerController.hidden);
}
}
I have the GameObject with the PlayerController script on it assigned to object B in the Inspector. So when I try to print the value of playerController.hidden…why is it printing that its false? I don’t understand where false is coming from??? I would at least think it would be either true, or maybe null if the value isn’t begin accessed properly… but why false?!
Well I have halfway answered my own questions. The reason it didn’t work is because I had put a prefab into the Player Controller field in the inspector. When I drag the instance of it already in the scene into that field, it works as expected.
So that brings a different question then… how can I do this with prefabs, so that my enemies just always access the player prefab that’s in any given scene? Is that possible?
ok, i made 2 empty game objects… put the playerController script on one, the enemyController script on the other, dragged the playerController object into the relevant field in the enemyContoller object and hit play.
Am I Hidden? True
set the playerController field in the enemyController script to none and it throws a null exception (not surprising).
the only way i can get it to say False is by unchecking the “hidden” bool on the playerController script.
This kind of assignment:
public class PlayerController : MonoBehaviour
{
public bool hidden = true;
only sets the value the very first time, if you interact with that bool in the inspector you will override it with what the inspector says it is… check what the hidden bool is set to in the inspector.
If you want to ignore the inspector settings you assign a value to the variable in the Awake() or Start() function.
public bool hidden;
// Use this for initialization
void Start ()
{
hidden = true;
}
You will want to use GetComponent to access the playerController script that is on the player object in the scene. And to automatically grab the player in the scene without manually dragging it into each enemy script, you will need to search for it.
In your EnemyController script
private PlayerController playerController;
void Start(){
var player = GameObject.Find("Player"); //or whatever your player is called
playerController = player.GetComponent<PlayerController>();
Debug.Log("Am I hidden? " + playerController.hidden);
}
There are other ways to search for Gameobjects in the scene as well, such as
Unless you want to manually drag the controller script of the player to every enemy in every scene, this is a better way to go.