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 <= nextFire))
{
nextFire = Time.time + fireRate;
}

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.

2 Answers

2

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;

the first thing worked for me thanks :wink:

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?

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

OK, 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!

should these 2 lines be in the same script?

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.

ok thanks for the help. managed to get it to work ;) just added a few more variables and lines because i had several scripts that needed to be toggled.