- Home /
Question by
S_Byrnes · Mar 16, 2014 at 12:40 PM ·
linkget component
How to Link Scripts Correctly?
Hey everyone, I need some help here, I have my main script with two elements that I need to refer to in my end game script, the two are numberOfLives and score, here is the end game script:
using UnityEngine;
using System.Collections;
public class Over : MonoBehaviour {
private bool gameOver = true;
public GUISkin guiSkin;
Rect windowRect = new Rect (0, 0, 620, 520);
//private bool toggleTxt = false;
//string stringToEdit = "Text Label";
//float hSliderValue = 0.0f;
//float vSliderValue = 0.0f;
//private float hSbarValuet;
//private float vSbarValue;
//private Vector2 scrollPosition = Vector2.zero;
void Start (){
Time.timeScale = 0.0f;
}
void Update (){
GetComponent ("GameLogicScript.cs");
windowRect.x = (Screen.width - windowRect.width) / 2;
windowRect.y = (Screen.height - windowRect.height) / 2;
if (numberOfLives <= 0)
{
Time.timeScale = 0.0f;
gameOver = true;
} else {
gameOver = false;
Time.timeScale = 1.0f;
}
}
void OnGUI(){
GUI.skin = guiSkin;
if (gameOver){
windowRect = GUI.Window (0, windowRect, OverMenu, "Game Over");
}
}
void OverMenu(int windowOver) {
if (GUI.Button ( new Rect(140,50,340,50), "Restart"))
Application.LoadLevel("TestScene1");
if (GUI.Button ( new Rect(140,120,340,50), "Main Menu"))
Application.LoadLevel ("Menu");
GUI.Button ( new Rect(140,190,340,50), "Options");
GUI.Button ( new Rect(140,260,340,50), "Score: " +score);
if (GUI.Button ( new Rect(140,330,340,50), "Quit"))
Application.Quit ();
}
}
If you need the other script, just let me know.
Also, is there anything I need to put in the other script to make these two link?
Thanks in advanced!
Comment
Best Answer
Answer by YoungDeveloper · Mar 16, 2014 at 12:43 PM
Using getcomponent each frame is not a good idea, pre-caching gameobject in start or awake would make much more sense.
I have explained getcomponent very clearly here: http://answers.unity3d.com/questions/606026/variable-from-other-gameobject.html
(There are two links in answer)