- Home /
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!
What exactly is not working when accessing the variable from the smoothfollow script? Does it give an error, or is it simply always false?
What does 'not work' mean? It is strange that you have:
@script AddComponent$$anonymous$$enu("Camera-Control/Smooth Follow")
But the code you have above is a modified version of the Smooth Follow script.
So sorry gentlemen, you are both right. Won't work isn't very descriptive. I'm not at my computer, so I can't give the exact error produced right now ( I will later) , but unity won't run the code because it doesn't recognize the EnemyAttack portions of the if statements in smoothfollow.js, however when I comment out these portions of smoothfollow.js and run it, the battle variable switches back and forth just fine. Could it have something to do with the fact that the target is the player object, and it's trying to call a script from an enemy object?
A bit more information: EnemyAttack.js is attached to the enemy object, and looks for whether or not the player has entered the enemy's detection trigger
Soundtrigger.js is attached to the game controller, which has the children BattleWild$$anonymous$$usic and Town$$anonymous$$usic
Smoothfollow.js is attached to the camera, which isn't a child of any other object. However smoothfollow.js is targeting the player character.
$$anonymous$$y overarching goal is to change the play style from third person Above behind to third person sidescroller upon entering battle. Think skyrim exploration meets smashbros combat. I simply got stuck at the part where the camera shifts. Thank you in advance for any help!
Answer by azmat786n · Jun 27, 2013 at 06:20 PM
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 :)
An additional tip: only use "method 1" (Send$$anonymous$$essage) in the few cases where "method 2" (dot notation) cannot be used. Send$$anonymous$$essage is slow and inelegant; comparable to repeatedly calling GameObject.Find when you could have just declared a variable.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to call Variable across scripts. 2 Answers
How to use a script on multiple gameobjects, then change a variable for one of them, not the other. 3 Answers
Easily cleaning up \ deleting unused variables? 2 Answers
how to detect if a non-moving object exist in a position? 1 Answer