Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by Jesus_Freak · Dec 23, 2010 at 02:07 AM · static-varschange variable

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) &amp;&amp; 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; } }

Comment
Add comment · Show 3
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 Jesus_Freak · Dec 23, 2010 at 02:09 AM 0
Share

i have no errors or NullReferenceExcrptions in the console....

avatar image AtomicMarine · Dec 29, 2010 at 01:42 PM 0
Share

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

avatar image AtomicMarine · Jan 04, 2011 at 10:05 PM 0
Share

I actually solved that problem i had above there :) ill post the solution soon :), but thanks for ur response

1 Reply

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

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.

Comment
Add comment · Show 2 · 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 Jesus_Freak · Dec 23, 2010 at 03:33 PM 0
Share

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....

avatar image JesusChristChangedMe · Dec 23, 2010 at 03:41 PM 1
Share

Welcome, two pair of eyes are always helpful.

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

No one has followed this question yet.

Related Questions

Disable gameobject at other screen with code ? 0 Answers

Change value of a projectile that is instantiated in Awake and pooled 1 Answer

Unityscript: Possible to access static variables dynamically? 1 Answer

Changing variables in GUI? 0 Answers

Is there a possible way to make gravity change when the character collides with something? 1 Answer


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