• 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
Question by laurienash · Aug 29, 2013 at 03:56 PM · booleanswitchfirst-person-controllerthirdpersoncontroller

Boolean check not referenced correctly from other script

Hello - in my game the player can switch between the first person controller and the third person controller on pressing the space bar.

On hitting a collider however, after three seconds it is switched to the first person controller automatically (without the player pressing space)

This works - but for some reason you have to press spacebar twice in order to switch back to the third person controller. I don't think I'm referencing the boolean check from the SwitchControllers script correctly - but I can't work out what I'm doing wrong.

This is the ForceIntoFirstPerson script (JavaScript):

 var cam01 : GameObject; // first person camera
 var cam02 : GameObject; // third person camera
 var player01 : GameObject; //first person controller
 var player02 : GameObject; //third person controller
 public static var check : boolean = false;
 
     
 function OnTriggerEnter(other: Collider){
 
 if (other.tag == "Player")
 {
   yield WaitForSeconds (3);
    
 cam01.gameObject.active = true;
 cam02.gameObject.active = false;
 player01.active = true;
 player02.active = false;
 check = true;
 }
 
 }

This is the Switch Characters script:

 var cam01 : GameObject; // first person camera
     var cam02 : GameObject; // third person camera
     var player01 : GameObject; //first person controller
     var player02 : GameObject; //third person controller
     public static var check;                 //  check-variable
  
     //start with first person active
     function Start() {
         check = true;
        cam01.gameObject.active = true; 
        cam02.gameObject.active = false; 
        player02.active = false;
        //check = true;
  
  
     }
  
  
     function Update() {
  
     player01.transform.position = player02.transform.position;
  
      if (Input.GetKeyDown ("space")) {
          if(check) {
          cam01.gameObject.active = false; 
          cam02.gameObject.active = true; 
          player01.active = false;
          player02.active = true;
        }
        else {
          cam01.gameObject.active = true; 
          cam02.gameObject.active = false; 
          player01.active = true;
          player02.active = false;
        }
     check = !check;
     }
     }

I tried this alteration (amongst many others), but it didn't work.

if (check) check = false; else check = true; }

Any help would be so, so much appreciated!! (I'm stuck until I sort this one out, and deadlines are scarily close)

Thanks so much, Laurien

Comment

People who like this

0 Show 2
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 meat5000 ♦ · Aug 30, 2013 at 09:54 AM 0
Share

You probably don't need a static variable.

avatar image laurienash · Aug 30, 2013 at 09:56 AM 0
Share

Do you know what I would need instead?

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by perchik · Aug 29, 2013 at 04:41 PM

Well you are correct, your boolean check is not being referenced correctly.

In your ForceIntoFirstPerson script, you need to have a reference to the switchCharacters script and set the 'check' variable of the switchCharacters script.

I'm not entirely sure of the Javascript version, but in C# it would be something like

  switchCharacters switchScript;
 
    Start(){
      ...
       switchScript = GetComponent<switchCharacters>();
     }
     ...
     switchScript.check = true;

Javascript is probably:

  var switchScript: switchCharacters;

  Start(){
     switchScript = GetComponenet<switchCharacters>();
     switchScript.check= true;
  }


Your other syntax, check =!check does exactly what your alternative says.

Comment
laurienash

People who like this

1 Show 5 · 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 laurienash · Aug 30, 2013 at 12:00 AM 0
Share

Hi - I've formatted it to this in the ForceIntoFirstPerson script - it doesn't throw any errors, but I still have to press spacebar twice after hitting the collider to get back to the first person controller.

In the error log, on pressing the spacebar once it says Null Reference Exception: Object reference not set to an instance of an object. It refers to this line : switchScript = GetComponent(SwitchCharacters).check;

  var cam01 : GameObject; // first person camera
     var cam02 : GameObject; // third person camera
     var player01 : GameObject; //first person controller
     var player02 : GameObject; //third person controller
     public static var check : boolean = false;
     var switchScript : SwitchCharacters;
     
     function Start () {
     
     
     switchScript = GetComponent(SwitchCharacters).check;
     switchScript.check = true;
     
     }
         
     function OnTriggerEnter(other: Collider){
     
     if (other.tag == "Player"){
     
     
       yield WaitForSeconds (3);
        
     cam01.gameObject.active = true;
     cam02.gameObject.active = false;
     player01.active = true;
     player02.active = false;
     check = true;
     }
     
     }

Is the variable being declared wrong do you think?

avatar image perchik · Aug 30, 2013 at 12:07 AM 1
Share

Don't use the local check at all in that script, when you said check=true that needs to be switchScript.check =true

also, this line switchScript = GetComponent(SwitchCharacters).check; needs to lose the .check at the end.

The reason it works when you press space twice is that the first time you press space, check is false so it gets changed to true. The second time you press space, check is true and the code runs. This means that it's not actually getting changed from the script you just included.

avatar image laurienash · Aug 30, 2013 at 12:19 AM 0
Share

Ok - thanks! I've changed it to this now, but I'm still having the same problem. I also still get the error Null Reference Exception as soon as the player is forced into first person - now at line 28 : switchScript.check = true;

  var cam01 : GameObject; // first person camera
     var cam02 : GameObject; // third person camera
     var player01 : GameObject; //first person controller
     var player02 : GameObject; //third person controller
     var switchScript : SwitchCharacters;
     
     function Start () {
     
     
     switchScript = GetComponent(SwitchCharacters);
     switchScript.check = true;
     
     }
         
     function OnTriggerEnter(other: Collider){
     
     if (other.tag == "Player"){
     
     
       yield WaitForSeconds (3);
        
     cam01.gameObject.active = true;
     cam02.gameObject.active = false;
     player01.active = true;
     player02.active = false;
     switchScript.check = true;
     }
     
     }
avatar image perchik · Aug 30, 2013 at 12:24 AM 0
Share

but not in Start() ?

avatar image laurienash · Aug 30, 2013 at 09:52 AM 0
Share

I've tried it in function Update already - but that doesn't work either.

avatar image

Answer by ThermalFusion · Aug 29, 2013 at 05:37 PM

The static variable 'check' in the two scripts are not the same variable, but two different. You probably mean to change SwitchCharacter's check in both scripts. Static variables still belong to the class they're declared in. Access them using ClassName.variableName. So in your case: SwitchCharacters.check = !SwitchCharacters.check.

Comment
laurienash

People who like this

1 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 laurienash · Aug 30, 2013 at 10:11 AM 0
Share

I've reformatted to this- but it's not working yet. I think it's the line switchScript.check = true; that isn't working, as I'm getting the error Object reference not set to instance of an object.

 var cam01 : GameObject; // first person camera
     var cam02 : GameObject; // third person camera
     var player01 : GameObject; //first person controller
     var player02 : GameObject; //third person controller
     var switchScript : SwitchCharacters;
     
     function Update () {
     
     
     var switchScript : SwitchCharacters = gameObject.GetComponent(SwitchCharacters);
     switchScript.check = true;
     
     }
         
     function OnTriggerEnter(other: Collider){
     
     if (other.tag == "Player"){
     
     
       yield WaitForSeconds (3);
        
     cam01.gameObject.active = true;
     cam02.gameObject.active = false;
     player01.active = true;
     player02.active = false;
     switchScript.check = !switchScript.check;
     }
     
     }

Any ideas? (thanks so much!)

I've made the change you suggested ThermalFusion - but it hasn't fixed it.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

19 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

Related Questions

Script that switches between first and third person controller 3 Answers

Switching between controllers - why do I have to press spacebar twice before it works? 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Reference boolean check from other script (not working) 1 Answer

Can't Set Animator Boolean Parameter C# 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