- Home /
 
Referencing booleans from settings
I have a scene in my game where an image shows up on screen. However, there are multiple images and the user selects which one he/she wants in the settings scene. I have two scripts dealing with this, ScaryImageScript (which instantiates the image) and ScaryImageChoice(which decides which image to use). To do this, I am using booleans. I only have the script for my first image ( I have two others), but want to run this before doing the others. I have tried looking at the documentation and other examples where GetComponent is used, but I don't feel that I grasp it yet. The console is telling me "NullReferenceException: Object reference not set to an instance of an object ScaryImageScript.Start () (at Assets/ScaryImageScript.js:16)" I am copying the example of accessing scripts from this Unity tutorial
I think the error has to do with my trying to access a boolean. Thank you for the help. Here is my code.
ScaryImageChoice.js
 #pragma strict
  
 
 var startingscaryimage1bool : boolean = true;
 var startingscaryimage2bool : boolean = false;
 var startingscaryimage3bool : boolean = false;
 
 var scaryimage1bool : boolean;
 var scaryimage2bool : boolean;
 var scaryimage3bool : boolean;
 
 function Start () {
 scaryimage1bool = startingscaryimage1bool;
 scaryimage2bool = startingscaryimage2bool;
 scaryimage3bool = startingscaryimage3bool;
 }
 
 
 function OnMouseUpAsButton () {
 ChangeBool11 ();
 ChangeBool12 ();
 ChangeBool13 ();
 }
 
 function ChangeBool11 () {
 if (scaryimage1bool == true) {
 Debug.Log("redundant");
 } else {
 scaryimage1bool = true;
 return scaryimage1bool;
 }
 }
 
 
 function ChangeBool12 () {
 if (scaryimage2bool == false) {
 Debug.Log("redundant");
 } else {
 scaryimage2bool = false;
 return scaryimage2bool;
 }
 }
 
 
 function ChangeBool13 () {
 if (scaryimage3bool == false) {
 Debug.Log("redundant");
 } else {
 scaryimage3bool = false;
 return scaryimage3bool;
 }
 }
 
 
 
               ScaryImageScript.js
 #pragma strict
 
 private var scaryImageChoice : ScaryImageChoice;
 
 var scaryimage1 : GameObject;
 var scaryimage2 : GameObject;
 var scaryimage3 : GameObject;
 var a = Vector3(0,0,0);
 
 function Awake () {
 scaryImageChoice = GetComponent(ScaryImageChoice);
 }
 
 
 function Start () {
 if (scaryImageChoice.scaryimage1bool == true) {
 var this1 : GameObject = Instantiate(scaryimage1, a, transform.rotation);
 } else if (scaryImageChoice.scaryimage2bool == true) { 
 var this2 : GameObject = Instantiate(scaryimage2, a, transform.rotation);
 } else if (scaryImageChoice.scaryimage3bool == true) {
 var this3 : GameObject = Instantiate(scaryimage3, a, transform.rotation);
 }
 }
 
               Thanks Again.
I think you need to make the variables (the one you want to access from the other scripts) public.
Example: public var startingscaryimage1bool : boolean = true; 
Answer by DylanWilson · May 12, 2014 at 05:15 AM
I believe you need to make the variables (the one you wish to access from the other script) public.
Example: public var startingscaryimage1bool : boolean = true; 
I thought unmarked variables are implicitly public in js. Correct me if I am wrong. The code did not run though
Your answer