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 frankyboy450 · Apr 24, 2012 at 04:58 PM · variableboolean

if several variables is true then do function

hi. i have this script


var fireRate = 1.0;
var projectile : Rigidbody;
var speed = 20;
var crashSound : AudioClip;
private var nextFire = 0.0;
public var TurretOn : boolean;

function Update() { if (Input.GetButtonDown("Fire1") && (Time.time

 if(Input.GetButton("Fire1") && (Time.time > nextFire)) 
 { 
     nextFire = Time.time + fireRate; 

     clone = Instantiate(projectile, transform.position, transform.rotation);
     clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
     audio.PlayOneShot(crashSound);
     Destroy (clone.gameObject, 3);
 }

}


i want to do so that the script only works if the "puclic var TurretOn:boolean" is true. how do i do this?

i also have another script with this line:


vehicle.GetComponent(VehicleControllScript).controlsEnabled = false;


how do i make it change the Boolean?

i know that i should only ask 1 question per post but this is for the same thing.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by BiG · Apr 24, 2012 at 05:17 PM

For the 1° question, change the first if-statement as follow:

 if (Input.GetButtonDown("Fire1") && (Time.time <= nextFire) && TurretOn)

The second question isn't clear, but maybe you want to do:

 temp = GameObject.Find("vehicle").GetComponent(VehicleControllScript).controlsEnabled;
 GameObject.Find("vehicle").GetComponent(VehicleControllScript).controlsEnabled = !temp;
Comment
Add comment · 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
0

Answer by frankyboy450 · Apr 27, 2012 at 04:47 PM

the first thing worked for me thanks ;)

sorry for being unclear with the second question.

This is my enter turret script that i have attached to a collider.

 private var weaponCamera : GameObject;              // drag and drop player camera from Hierarchy to Inspector window!    
 var vehicleCam : GameObject;              
 var vehicleCameraTarget : Transform;  
 var vehicle : GameObject;   
 private var Player;  
 var GetOutPosition : Transform;                        // Empty game object, where player will get out of the vehicle    
 var VehicleControllScript: String = "ScriptName";       // Just write script name, which controls vehicle movement (controller script).      
 private var opened : boolean = false;  
 private var waitTime : float = 1;                       // leave it as 1   
 private var temp : boolean = false;  
 private var mainCamera : GameObject;  
 
 
 
 
 function Start () {
     Player = GameObject.FindWithTag("Player");         
     mainCamera = GameObject.FindWithTag("MainCamera");
     weaponCamera = GameObject.FindWithTag("WeaponCamera"); 
     vehicleCam.camera.enabled = false;
         vehicle.GetComponent(VehicleControllScript).TurretEnabled = false;
     vehicleCam.GetComponent(AudioListener).enabled = false;  
 }
 
 function Update() {
     if ((Input.GetKeyDown("e")) && opened && !temp){
         GetOut();
         opened = false;
         temp = false;
     }
 }
 
 function Action (){
     if (!opened && !temp){
         GetIn();
         opened = true;
         temp = true;
         yield WaitForSeconds(waitTime);
         temp = false;
     }
 }
 
 
 function GetIn() {
     var changeTarget : VehicleCamera = vehicleCam.transform.GetComponent("VehicleCamera");
     changeTarget.target = vehicleCameraTarget;
     Player.BroadcastMessage("LightOff");
     
     // Disable all script behaviours on Player (Essentially deactivating player control)
     var coms : Component[] = Player.GetComponentsInChildren(MonoBehaviour);
     for (var b in coms) {
         var p : MonoBehaviour = b as MonoBehaviour;
         if (p)
             p.enabled = false;
     }
     
     // Disable all renderers
     var gos = Player.GetComponentsInChildren(Renderer);
     for( var go : Renderer in gos){
         go.enabled = false;
     }
     
     Player.transform.parent = vehicle.transform;
     Player.transform.position = vehicleCameraTarget.transform.position;
     Player.rigidbody.isKinematic = true;
     Player.collider.isTrigger = true;
     weaponCamera.camera.enabled = false;
     weaponCamera.GetComponent(AudioListener).enabled = false;
     mainCamera.camera.enabled = false;
     vehicleCam.camera.enabled = true;
     vehicle.GetComponent(VehicleControllScript).TurretEnabled = true;
     vehicleCam.GetComponent(AudioListener).enabled = true;
 }
 
 
 function GetOut() {
     
     // Enable all script behaviours on Player (Essentially deactivating player control)
     var coms : Component[] = Player.GetComponentsInChildren(MonoBehaviour);
     for (var b in coms) {
         var p : MonoBehaviour = b as MonoBehaviour;
         if (p)
             p.enabled = true;
     }
     
     // Enable all renderers
     var gos = Player.GetComponentsInChildren(Renderer);
     for( var go : Renderer in gos){
         go.enabled = true;
     }
 
     Player.transform.parent = null;
     Player.rigidbody.isKinematic = false;
     Player.collider.isTrigger = false;
     Player.transform.position = GetOutPosition.transform.position;
     weaponCamera.camera.enabled = true;
     weaponCamera.GetComponent(AudioListener).enabled = true;
     mainCamera.camera.enabled = true;
     vehicleCam.camera.enabled = false;
     vehicleCam.GetComponent(AudioListener).enabled = false;
     vehicle.GetComponent(VehicleControllScript).TurretEnabled = false;
 }


here's my shooting script added inside the turret model (the barrel of it)


 var fireRate = 1.0;   
 var projectile : Rigidbody;  
 var speed = 20;  
 var crashSound : AudioClip;  
 private var nextFire = 0.0;  
 public var TurretEnabled : boolean;  
 
 function Update()
 {
     if (Input.GetButtonDown("Fire1") && (Time.time <= nextFire) && TurretEnabled) 
     { 
         nextFire = Time.time + fireRate; 
     } 
 
     if(Input.GetButton("Fire1") && (Time.time > nextFire) && TurretEnabled) 
     { 
         nextFire = Time.time + fireRate; 
 
         clone = Instantiate(projectile, transform.position, transform.rotation);
         clone.velocity = transform.TransformDirection( Vector3 (0, 0, speed));
         audio.PlayOneShot(crashSound);
         Destroy (clone.gameObject, 3);
     }
 }


how will i do so that the enter turret script changes the "TurretOn" boolean variable to on and off?

and changes the camera?

Comment
Add comment · Show 6 · 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 Lo0NuhtiK · Apr 27, 2012 at 04:54 PM 4
Share

$$anonymous$$Y EYES!! THEY BLEED!

avatar image frankyboy450 · Apr 27, 2012 at 05:53 PM 0
Share

oh yeah. sorry. don't know how to add code in in that special way so that they show easier

avatar image BiG · Apr 27, 2012 at 06:44 PM 2
Share

O$$anonymous$$, you have to write:

 temp = GameObject.Find("Turret").GetComponent(Shooting_script).TurretEnabled;    

 GameObject.Find("Turret").GetComponent(Shooting_script).TurretEnabled = !temp;

,inside the collider script. The first line detects the truth value for TurretEnabled (true/false), and the second line change that value with the opposite.

ATTENTION!!! The names "Turret" and "Shooting_script" have been used by me to indicate those particular items. Change them with the names that you have used for them. I hope that it's clear!

avatar image frankyboy450 · Apr 29, 2012 at 02:05 PM 0
Share

should these 2 lines be in the same script?

avatar image BiG · Apr 29, 2012 at 02:34 PM 0
Share

Yes. Exactly as they are written here (but with the names changed, as I said above). They go inside the collider script and they'll change TurretEnabled value.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Multiple objects sharing a boolean variable in a script. 1 Answer

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

Networking,State synchronize variable and function 1 Answer

Help with my code?(NullReferenceException)[CLOSED] 2 Answers


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