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 laurienash · Jun 12, 2013 at 12:59 PM · booleanfirst-person-controllercheck

Reference boolean check from other script (not working)

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
 
     
 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;
 var temp : SwitchCharacters = gameObject.GetComponent(SwitchCharacters);
     temp.check = true;
 
 }
 
 } 

And this is the SwitchCharacters 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
     var check;                 //  check-variable
  
     //start with first person active
     function Start() {
        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;
     }
  
  
   }

Could anyone help - I just can't work out how to solve this problem at all.

Thanks, Laurien

Comment
Add comment · Show 12
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 SubatomicHero · Jun 12, 2013 at 01:14 PM 0
Share

I think you've added the same script twice :D

avatar image saschandroid · Jun 12, 2013 at 01:15 PM 0
Share

Sorry ... you posted the same code for both scripts.

avatar image laurienash · Jun 12, 2013 at 01:25 PM 0
Share

Sorry - I've edited (oops)

avatar image SubatomicHero · Jun 12, 2013 at 01:39 PM 0
Share

ok first move:

 check = !check;

to the line below your if statement checking for the space bar going down and before if(check).

second change Input.Get$$anonymous$$eyDown to Input.Get$$anonymous$$eyUp. Get$$anonymous$$eyDown I believe is when you hold the space bar down rather than just pressing it once.

avatar image laurienash · Jun 12, 2013 at 02:37 PM 0
Share

If I change the script like this:

 function Update() {
  
     player01.transform.position = player02.transform.position;
  
      if (Input.Get$$anonymous$$eyDown ("space")) {
    check = !check;
        if(check) {

It stops working at all, and the player can't switch between the first person and third person controller. Is that what you meant?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by OP_toss · Jun 13, 2013 at 10:57 PM

Hmmm... Let me see if I can give you some pointers.

Here's the simple fix:

Make a method in SwitchCharacters called Switch(). This will perform the switch and handle the bool etc.

 public void Switch()
 {
   check = !check; //toggle
   cam01.gameObject.active = !check;
   cam02.gameObject.active = check;
   player01.active = !check;
   player02.active = check;
 }

From SwitchCharacters call Switch():

 if (Input.GetKeyDown ("space")) { //only happens once per push
   Switch();
 }


Then from ForceIntoFirstPerson call Switch():

 if (other.tag == "Player")
 {
   yield WaitForSeconds (3);
  
   var switcher : SwitchCharacters = gameObject.GetComponent(SwitchCharacters);
   switcher.Switch();
 }

Now all your functional code is in one place. This is still very basic and simplistic, but should suite your needs fine. Never have redundant code if it can be avoided.

I strongly suggest you change your variable and class names. For instance, "check" is extremely ambiguous. Instead use something like isFirstPerson or inversely isThirdPerson. This tells you something at least. Even better would be an enum with 2 options like { FIRST_PERSON, THIRD_PERSON }. Also your class names shouldn't be verbs, generally. They are nouns, and what they do is verbs (methods). So something like PlayerCameraSwitcher, or CameraSwitchController.

Hope this helps!

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 laurienash · Aug 29, 2013 at 03:46 PM 0
Share

Hi, this is probably too late now - I've been in hospital for quite a while.

I'm having problems with the script advice you've given as I think its in C sharp? I've tried writing the equivalent in JavaScript, but I'm getting a lot of errors back.

Thanks for the advice on variables and class names!

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

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

Trying to find boolean value in another script 0 Answers

Boolean check not referenced correctly from other script 2 Answers

A node in a childnode? 1 Answer

C# Boolean Doesn't Change Value 1 Answer

Switching between controllers - why do I have to press spacebar twice before it works? 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