- Home /
static vars in another script not changing?
hey, i have 2 scripts that i made, but one is a collection of some code i made, some might be part of the tornado twin tutorials, some might be from other scripts floating around Unity Answers and Unity forums... but it doesn't really matter... i give full credit to the people who made the code. anyway, i have a static var in the FMoveAround script(1st below) and i (try to) call it in another script: Tutorial script. but when i play the game... i can move the player around and instantiate the fireballs and 'drag' my player.... on start! which according to the Tutorials script, i don't want to Takeinput on start. can you please help me figure out why FMoveAround is ignoring Tutorial's desire to alter the value of 'Takeinput'?
thanks for your help in advance!
player var's values in the inspector (from the FMoveAround script):
Button Width = 200
Button Height = 100
Cam = Camera = main camera
Col = yellow
fast = true
Projectile speed = 105
ortho = true
Bspeed = 1500
fire = true
far = 500
Rect1 = x = 50, y = 46.20723 Width = 10 Height = 500
val2 = 198.4793
val 3 = 5
Level = -5
all 3 OOB's = 1000
target cam = Camera with compass texture + script to rotate GUILabel based on the cam's y rotation angles.
scripts: FMoveAround-
import System; var buttonWidth : int; var buttonHeight : int; private var MovedToPoint = false; private var MovedToPointTrue = false; var offset : Vector2 = Vector2.zero; var HERE : Vector3; var Val : int; var cam : Camera; var col : Color; var Spike : Transform; var work = true; var cnt : int; var Fast = false; static var message : String = "My Words"; private var gravitySave : Vector3; private var gravityOn = true; var projectileSpeed : int = 105; private var faster : int;
//vars var ortho = false; var bullitPrefab : Transform; var respawn1 : Vector3; var respawn2 : Vector3; var ctrl : int = 100; var RespawnToHere : Vector3; var Bspeed : int = 1500; var fire = true; var far : int; var LevelOne = true;
//private vars private var Respawn = true;//first respawn private var auto = false; //auto move? private var constant = false; private var cont = false; private var rad : int = 0; private var Mywords : String = "Don't run away!"; //static vars static var gotHit = false; static var UseBetterBullet = false; //flamethrower static var dead = false;//1st time dying static var Dead = false;//2nd time dying static var money = 0; static var SCORE = 0;
private var showGUI = false;
function Start() { var sources = FindObjectsOfType(AudioSource); for(var so : AudioSource in sources) { if(so == GameObject.FindWithTag("SFXHolder").GetComponent(typeof(so))) { so.volume = so.volume Options.effectsVolume; } if(so == GameObject.FindWithTag("wormProjectile").GetComponent(typeof(so))) { so.volume = so.volume Options.effectsVolume; } if(so == null) { Debug.LogWarning("There is no audio in this scene, liven it up a bit!"); } else if(so !== GameObject.FindWithTag("SFXHolder").GetComponent(typeof(so))) { source = 1 * Options.musicVolume; } } }
function DragResist() { if(clicks >= 2) { rigidbody.drag = 0; } }
function Awake() { HERE = transform.position; } //late update function that corrects anything that might need to be fixed before next script loop read. function LateUpdate() { if(PlayerHeliPlayer.DoneFlying) { transform.position = RespawnToHere; PlayerHeliPlayer.DoneFlying = false; } if(Dead && !Respawn) //second respawn { transform.position = respawn2; Dead = false; print("i respawned yet again!"); } if(dead && Respawn) //first respawn {//where the player respawns transform.position = respawn1; dead = false; print("i respawned!"); } }
//on trigger function for when player hits the specified triggers. function OnTriggerEnter( hit : Collider ) { if(hit.gameObject.tag == "SuddenDeath1") { enemyFollow.MOVE1 = false; enemyFollow.MOVE2 = true; enemyFollow.MOVE3 = false; }
if(hit.gameObject.tag == "SuddenDeath2") { enemyFollow.MOVE1 = false; enemyFollow.MOVE2 = false; enemyFollow.MOVE3 = true; }
if(hit.gameObject.tag == "MoneyMaker") { money += 50; Destroy(hit.gameObject); print("I have " +money+ " Dollars!"); }
if(hit.gameObject.tag == "fallout3" && !Respawn) { Dead = true; HP.LIVES -= 1; } if(hit.gameObject.tag == "fallout2") { Respawn = false; } //when the player falls off (this doesnt really work too good. ) if(hit.gameObject.tag == "fallout") { dead = true; //subtract life here HP.LIVES -= 1; print("i fell off..."); }
//getting hit (this works) if(hit.gameObject.tag == "enemyProjectile") { gotHit = true; HP.HITS += 1; Destroy(hit.gameObject); print("i got hit!"); } //manipulating weather if(hit.gameObject.tag == "weather") { ParticleEmitToggle.WCONTROL = true; print("Now i can control weather."); } //level select button controls if(hit.gameObject.tag == "Level2") { GUIshader.Level2 = true; print("hit level two"); }
if(hit.gameObject.tag == "Level3") { GUIshader.Level3 = true; print("hit level three"); }
if(hit.gameObject.tag == "Level4") { GUIshader.Level4 = true; print("hit level four"); }
if(hit.gameObject.tag == "Level5") { GUIshader.Level5 = true; print("hit level five"); }
if(hit.gameObject.tag == "WORK") { MovingPlatform4.work = true; } } var Rect1 : Rect = Rect(50,46.20723,10,500); var val2 : float = 300; var val3 : float = 2.000685; var clicks : int = 0; //GUI function to print the time the player spent playing && money && Orthographical_Zoom function OnGUI() { GUI.contentColor = col; GUI.Label(Rect(20,100, 400,30), "Play Time: " +System.DateTime.Now);
GUILayout.Label("Money: $" + money); if(!ortho) { Camera.main.fieldOfView = GUI.VerticalSlider(Rect1, Camera.main.fieldOfView,val2,val3); } if(ortho) { Camera.main.orthographicSize = GUI.VerticalSlider(Rect1, Camera.main.orthographicSize,val2,val3); } if(showGUI) { Takeinput = false; GUILayout.BeginArea(Rect((Screen.width / 2) - (buttonWidth / 2), (Screen.height / 2) - (buttonHeight / 2 + 20), buttonWidth, buttonHeight)); GUILayout.Label("Double Click:"); GUILayout.EndArea (); GUILayout.BeginArea(Rect((Screen.width / 2) - (buttonWidth / 2), (Screen.height / 2) - (buttonHeight / 2), buttonWidth, buttonHeight)); if(GUILayout.Button(Mywords)) { clicks++; rigidbody.velocity = Vector3.zero; rigidbody.drag = 100; dead = true; if(clicks >= 2) { //subtract life here HP.LIVES -= 1;
} DragResist(); showGUI = false; } GUILayout.EndArea (); } } var Level : int; var OOBtop : int; //OOB == out of bounds! var OOBright : int; var OOBforward : int; static var Takeinput : boolean = true; var targetCam : Transform; //code that will be read every loop (every frame) function Update() { if(Level != 1) { transform.localEulerAngles.y = targetCam.localEulerAngles.y; } if(Input.GetKeyDown("o") && !ortho) { ortho = true; Camera.main.orthographic = true; } if(Input.GetKeyDown("p") && ortho) { ortho = false; Camera.main.orthographic = false;
} if(!showGUI) { Takeinput = true; } if(Level != 1) { if(transform.position.z >= OOBforward) { showGUI = true; } if(transform.position.z <= -OOBforward) { showGUI = true; } if(transform.position.y <= -OOBtop) { showGUI = true; } if(transform.position.y >= OOBtop) { showGUI = true; } if(transform.position.x >= OOBright) { showGUI = true; } if(transform.position.x <= -OOBright) { showGUI = true; } } if(Level == 1) { if(transform.position.y >= OOBtop) { showGUI = true; } if(transform.position.y <= -OOBtop) { showGUI = true; } if(transform.position.z >= OOBright) { showGUI = true; } if(transform.position.z <= -OOBright) { showGUI = true; } }
if(Takeinput && !ortho) { if(Input.GetKey(KeyCode.Mouse1)) { if(offset == Vector2.zero) offset = new Vector2(transform.position.x - Input.mousePosition.x,transform.position.z - Input.mousePosition.y); transform.position.x = Input.mousePosition.x + offset.x; transform.position.z = Input.mousePosition.y + offset.y; } else { offset = Vector2.zero; }
} if(!MovedToPoint) { if(Takeinput && ortho) { if(Input.GetKey(KeyCode.Mouse1)) {
if(offset == Vector2.zero)
offset = new Vector2(transform.position.z - Input.mousePosition.x, transform.position.y - Input.mousePosition.y);
transform.position.z = Input.mousePosition.x + offset.x;
transform.position.y = Input.mousePosition.y + offset.y;
}else{offset = Vector2.zero;} MovedToPoint = true; MovedToPointTrue = true; }
} if(MovedToPointTrue) { MovedToPoint = false; }
//print(offset); print(Input.mousePosition); //if(Input.GetAxis("Mouse ScrollWheel")) //{ //Camera.main.orthographic = true; //Camera.main.orthographicSize -= (Input.GetAxis("Mouse ScrollWheel") 500) Time.deltaTime; //print(Input.GetAxis("Mouse ScrollWheel")); //} if(Input.GetKeyDown("left shift") && Takeinput) { cnt++; } if(cnt == 0) { faster = 2; } if(cnt == 1) { faster = 20; Fast = true; } if(cnt == 2) { faster = 2; Fast = false; } if(cnt >= 3) { cnt = 0; Fast = false; } if (Input.GetKey(KeyCode.Mouse0) && Takeinput) { var ray = Camera.main.ScreenPointToRay (Input.mousePosition); // Construct a ray from the current mouse coordinates var hit : RaycastHit;
if (Physics.Raycast (ray, hit) && Takeinput)
{
Debug.DrawLine (Camera.main.transform.position, hit.point, Color.blue);
Debug.Log(hit.point);
var bullit2 = Instantiate (bullitPrefab, hit.point, transform.rotation); // Create a particle if hit
bullit2.tag = "wormProjectile";
hit.transform.SendMessage("RCastHit", gameObject,SendMessageOptions.DontRequireReceiver);
}
}
if(work && ortho) { if(transform.position.z >= far) { transform.position.z = far - 20; } if(transform.position.z <= (-1 far)) { transform.position.z = (far -1) + 20; } }
Debug.Log(transform.position); if(transform.position.x != 0 && ortho && Level == 1) { transform.position.x = 0; } if(Level == 1 && Takeinput && ortho) { rigidbody.AddRelativeForce(0, ((Input.GetAxis("Vertical") 500) faster) Time.deltaTime, ((Input.GetAxis("Horizontal") 500) faster) Time.deltaTime); } if(Level == 1 && Takeinput && ortho) { rigidbody.AddRelativeForce(0,((Input.GetAxis("Vertical") 500) faster) Time.deltaTime, ((Input.GetAxis("Horizontal") 500) faster) Time.deltaTime); } if(Level != 1 && Takeinput && ortho) { rigidbody.AddRelativeForce(((Input.GetAxis("Horizontal") 500) faster) Time.deltaTime, ((Input.GetAxis("Forward") 5000) faster) Time.deltaTime,((Input.GetAxis("Vertical") 500) faster) Time.deltaTime); } if(Level != 1 && Takeinput && !ortho) { rigidbody.AddRelativeForce(((Input.GetAxis("Horizontal") 500) faster) Time.deltaTime, ((Input.GetAxis("Forward") 5000) faster) Time.deltaTime,((Input.GetAxis("Vertical") 500) faster) Time.deltaTime); }//forward is just the name of the axis holding the keys q and e.
//main menu button if(Input.GetKeyDown("escape")) { Application.LoadLevel("Main Menu"); print("Main Menu Loading..."); }
//shooting
if(Input.GetKeyDown("f") && Takeinput && !ortho) { if(UseBetterBullet) { particleEmitter.emit = true; InvokeRepeating("DestroyEmitter",1,1);
} }
//prints your score print("Score: "+SCORE); //changes your score switch(SCORE) { case 10: Application.LoadLevel("Main Menu"); break; }
}
//my function to destroy the flamethrower PE over time. PE = ParticleEmitter. function DestroyEmitter() { if(Time.time*1.5 > 7.5) particleEmitter.emit = false; }
Tutorials-
var firstMessage : String; var secondMessage : String; var thirdMessage : String; var firstAct : boolean = true; var secondAct : boolean = false; var thirdAct : boolean = false; private var msgToDisplay : String; var clicks : int = 0; var buttonWidth : int; var buttonHeight : int;
function Awake() { FMoveAround.Takeinput = false; msgToDisplay = firstMessage; clicks = 0; first = true; second = false; third = false; } function Start() { if(FMoveAround.Takeinput) { FMoveAround.Takeinput = false; } }
function OnGUI() {
GUI.skin.button.wordWrap = true;
if(GUI.Button(Rect((Screen.width / 2) - (buttonWidth / 2), (Screen.height / 2) - (buttonHeight / 2), buttonWidth, buttonHeight), msgToDisplay)) { clicks++; } }
function Update() { switch(clicks) { case 1: msgToDisplay = secondMessage; FMoveAround.Takeinput = false; break; case 2: msgToDisplay = thirdMessage; FMoveAround.Takeinput = true; break; } if(clicks >= 3) { FMoveAround.Takeinput = false; clicks = 0; } }
i have no errors or NullReferenceExcrptions in the console....
Hey $$anonymous$$an :) Remember me? well you said if I had some scripting problems i could leave a comment on one of your questions >.< haha so I did, anyway here is the question and subsequently the problem I have been having
http://answers.unity3d.com/questions/33960/make-player-face-where-joystick-is-pointing
:) Thanks in advance - Dom
I actually solved that problem i had above there :) ill post the solution soon :), but thanks for ur response
Answer by JesusChristChangedMe · Dec 23, 2010 at 02:36 PM
The problem is that takeinput always = true. If(!showGUI) take input = true; and that is returning Takeinput to true no matter what as long as showGUI is false.
wow, thanks! i didn't even notice that! it sure would explain why i could move around when i said in another script not to....
Welcome, two pair of eyes are always helpful.
Your answer
Follow this Question
Related Questions
Advice on static variables and object references 1 Answer
Accesing instantiated gameobjects from another script 2 Answers
How to change a variable while playing 1 Answer
How do I change the logged text based on a momentary collision? 1 Answer
how to make attack for multiple weapons(with change,not at once) 0 Answers