Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by JacobHockey13 · May 12, 2014 at 05:08 AM · getcomponentbooleanreference

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.

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DylanWilson · May 12, 2014 at 05:14 AM 0
Share

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;

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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;

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image JacobHockey13 · May 12, 2014 at 01:57 PM 0
Share

I thought unmarked variables are implicitly public in js. Correct me if I am wrong. The code did not run though

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Getting NullReferenceException? info inside 1 Answer

getting a variable from another script problem 1 Answer

Referencing a c# component script from JS script 2 Answers

How Activating a boolean fron another script(C#) 1 Answer

Reference Not Set toInstance of Object 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges