Jump to content
This site uses cookies! Learn More

This site uses cookies!

armedunity.com uses cookies to improve user experience.

By continuing to use this site, you agree to allow us to store cookies on your computer.

  • 0
mgudoy

How to use static variable in another script

Question

This Hole.js
var SpawnPoint : Transform;
public var PowerMeter;
var Pamato : GameObject;
var Lives = 3;
var objectInArea : boolean = false;
var waitTime : float = 5.0;
private var savedWaitTime : float;
var GUIScore : GUIText;
var GameOver : String;
var Life3 : GUITexture;
var Life2 : GUITexture;
var Life1 : GUITexture;
 
 
 
function Start(){
Life3.enabled = true;
Life2.enabled = true;
Life1.enabled = true;
savedWaitTime = waitTime;
}
 
function OnTriggerEnter(col : Collider){
if(col.tag == "GameController"){
objectInArea = true;
 
}
}
 
function OnTriggerExit(col : Collider){
     if(col.tag == "GameController"){
objectInArea = false;
savedWaitTime = waitTime;
     }
}
 
function Update(){
if(objectInArea){
if(savedWaitTime > 0.0){
savedWaitTime  -= Time.deltaTime;
if(savedWaitTime <= 0.0){
Pamato.transform.position = SpawnPoint.position;
   Lives -= 1; 
   
   
objectInArea = false;
savedWaitTime = waitTime;
if(Lives == 2){
Life3.enabled = false;
Life2.enabled = true;
Life1.enabled = true;
 
 
}
if(Lives == 1){
Life3.enabled = false;
Life2.enabled = false;
Life1.enabled = true;
}
if(Lives == 0){
Life3.enabled = false;
Life2.enabled = false;
Life1.enabled = false;
   GameOverRetry();
 
 
}
}
}
}
}
function GameOverRetry(){
yield WaitForSeconds(2);
Application.LoadLevel(GameOver);
}
 
This is my SpawnPoint.js
var SpawnPoint : Transform;
public var PowerMeter;
var Pamato : GameObject;
var Lives = 3;
var objectInArea : boolean = false;
var waitTime : float = 5.0;
private var savedWaitTime : float;
var GUIScore : GUIText;
var GameOver : String;
var Life3 : GUITexture;
var Life2 : GUITexture;
var Life1 : GUITexture;
 
 
 
function Start(){
Life3.enabled = true;
Life2.enabled = true;
Life1.enabled = true;
savedWaitTime = waitTime;
}
 
function OnTriggerEnter(col : Collider){
if(col.tag == "GameController"){
objectInArea = true;
 
}
}
 
function OnTriggerExit(col : Collider){
     if(col.tag == "GameController"){
objectInArea = true;
savedWaitTime = waitTime;
     }
}
 
function Update(){
if(objectInArea){
if(savedWaitTime > 0.0){
savedWaitTime  -= Time.deltaTime;
if(savedWaitTime <= 0.0){
Pamato.transform.position = SpawnPoint.position;
   Lives -= 1; 
    
    
objectInArea = false;
savedWaitTime = waitTime;
if(Lives == 2){
Life3.enabled = false;
Life2.enabled = true;
Life1.enabled = true;
 
 
}
if(Lives == 1){
Life3.enabled = false;
Life2.enabled = false;
Life1.enabled = true;
}
if(Lives == 0){
Life3.enabled = false;
Life2.enabled = false;
Life1.enabled = false;
   GameOverRetry();
 
 
}
}
}
}
}
function GameOverRetry(){
yield WaitForSeconds(2);
Application.LoadLevel(GameOver);
}

 

I want that my lives to will change to the same script when the object hitted the collider 

it will minus the lives of both scripts 

Share this post


Link to post
Share on other sites

13 answers to this question

Recommended Posts


  • 0

I tried it earlier but the lives is malfunctioning :( I'm working for this in 2days and im stuck :(

Share this post


Link to post
Share on other sites
  • 0

The better way to understand this function is here - see this video ++

Please login or register to see this link.

Share this post


Link to post
Share on other sites
  • 0

Dude, YoungDeveloper explains it very well with an example provided, what don't you get? Not everything is going to get handed to you, you can't count on others to fix your problems completely.

 

If you put your script in code tags then I'll help. Otherwise, I'm not reading that blob of crap.

  • Like 1

Share this post


Link to post
Share on other sites
  • 0

You run GetComponent on the GameObject that has the script you want to access.

Share this post


Link to post
Share on other sites
  • 0

The GetComponent is not working for me because my variable has an integer. (var Lives = 3;)

So what I did is...

SpawnPoint.js

I made my variable public static variable

(public static var Lives = 3;) - This means that the variable is connected to any script when called

then 

Hole.js

I made also the same method and call the SpawnPoint script and use in the function of Lives

 

 SpawnPoint.Lives -= 1; 
    
    
objectInArea = false;
savedWaitTime = waitTime;
if(SpawnPoint.Lives == 2){
Life3.enabled = false;
Life2.enabled = true;
Life1.enabled = true;
 
 
}
if(SpawnPoint.Lives == 1){
Life3.enabled = false;
Life2.enabled = false;
Life1.enabled = true;
}
if(SpawnPoint. == 0){
Life3.enabled = false;
Life2.enabled = false;
Life1.enabled = false;
   GameOverRetry();

 and it went well 

Sorry for annoying you all! :d but thanks in helping guys! ^_^

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...