Difficulty Calling Variables from other scrtipts

Hi there, pretty new to unity here. I was wondering if anyone could help me understand what I’m doing wrong. I have a script attached to an enemy, EnemyAttack.js that looks something like this:

 #pragma strict
    
    private var Player : GameObject;
    static var battle : boolean;
    private var BattleTrigger = boolean;
    
    function Awake(){
    Player = GameObject.FindGameObjectWithTag("Player");
    }
    
    
    function OnTriggerStay (other : Collider) {
           
           if(other.gameObject == Player){
             battle = true;     
        }
        }
        
    function OnTriggerExit (other : Collider) {
    
    		if(other.gameObject == Player){
             battle = false;
             
         }
         }
        
    function BattleFunction ( BattleTrigger : boolean) {
    
    }

This determines whether or not the Player is fighting someone. so then I have a script soundtriggers.js which switches the music based on the battle state, and this WORKS. As in, when the character is in range of an enemy, battle music plays, when the character leaves it stops.

#pragma strict

var musicFadeSpeed : float = 1f;
static var battle : boolean;
var BattleWildMusic : AudioSource;
var TownMusic : AudioSource;

function Awake(){

	BattleWildMusic = transform.FindChild("BattleWildMusic").audio;
	TownMusic = transform.FindChild("TownMusic").audio;
}

function Update() {
	
	BattleWildMusic.Pause();
	
	if (EnemyAttack.battle == true){
	
		TownMusic.volume = 0.0;
		BattleWildMusic.volume = 1.0;
		BattleWildMusic.Play();
		
	}
	
	else{
	
		TownMusic.volume = 1.0;
		BattleWildMusic.volume = 0.0;
		BattleWildMusic.Stop();

	}
	
	
	}

so now I try to access the ‘battle’ variable one more time with the script smoothfollow.js, and I can’t for the life of me get it to work!

var target : Transform;
static var battle : boolean;
// The distance in the x-z plane to the target
var distance = 10.0;
// the height we want the camera to be above the target
var height = 5.0;
// How much we 
var heightDamping = 2.0;
var rotationDamping = 3.0;

var distanceMin = 10;
var distanceMax = 100;
var scrollspeed = 0.0005;
var ySpeed = 120.0;
var yMinLimit = 0;
var yMaxLimit = 80;


// Place the script in the Camera-Control group in the component menu
@script AddComponentMenu("Camera-Control/Smooth Follow")


function Update () {

	
	if (EnemyAttack.battle == true) {
	print("BATTLING");
	}
	// Early out if we don't have a target
	if (!target){
		return;
		}
	
	if (EnemyAttack.battle == false){
			
	if(Input.GetMouseButton(0)){
		height =  height + Input.GetAxis("Mouse Y") * ySpeed;
		height = Mathf.Clamp(height,yMinLimit,yMaxLimit);
		}
	
	height = Mathf.Clamp(height,yMinLimit,yMaxLimit);
		
	// Calculate the current rotation angles
	var wantedRotationAngle = target.eulerAngles.y;
	var wantedHeight = target.position.y + height;
		
	//var currentRotationAngle = transform.eulerAngles.y;
	var currentHeight = transform.position.y;
	var currentRotationAngle = transform.eulerAngles.y;
	
	distance = Mathf.Clamp(distance - Input.GetAxis("Mouse ScrollWheel")*scrollspeed, distanceMin, distanceMax);
	
	// Damp the rotation around the y-axis
	currentRotationAngle = Mathf.LerpAngle (currentRotationAngle, wantedRotationAngle, rotationDamping * Time.deltaTime);

	// Damp the height
	currentHeight = Mathf.Lerp (currentHeight, wantedHeight, heightDamping * Time.deltaTime);

	// Convert the angle into a rotation
	var currentRotation = Quaternion.Euler (0, currentRotationAngle, 0);
	
	// Set the position of the camera on the x-z plane to:
	// distance meters behind the target
	transform.position = target.position;
	transform.position -= currentRotation * Vector3.forward * distance;

	// Set the height of the camera
	transform.position.y = currentHeight;
	
	// Always look at the target
	transform.LookAt (target);
	}
	
	
	//if(EnemyAttack.Battle == true) {
	//transform.position = Vector3(0,0,0);
	 // }
	
}

I would really appreciate any help!

hy use this

//object1->Script1

var obj2 : GameObject;
var script2 : Script2;

function Start() {
    //get object and component
    obj2 = GameObject.Find("object2");
    script2 = obj2.GetComponent(Script2);

    //calling methods
    //method one
    script2.sendMessage("changeName");
    //method two
    script2.changeName();

    //calling methods with parameters
    script2.sendMessage("changeName", Use String);
    script2.changeName(Use String);
    
    //calling return type functions
    var name = script2.getName();
}



//object2->Script2

var myName : String;

//simple function
public changeName function() {
    myName = "Azmat Ali Meer";
}

//function with parameters
public changeName function(name:String) {
    myName = name;
}

//return type function
public getName String() {
    return "Azmat Ali Meer":
}

Hope this help you and others :slight_smile: