Ok so i just made this script and added it to a new Empty Game Object, it all works fine “no errors” the first Image appears as it should. I press space bar and the sound plays and the texture is replaced with the 2’nd in line. And so on, but when it reaches the 4’th texture “last one” when i press space it wont reset back to the default texture 1.
So it will loop through all the pages, just in case they hit space twice and missed a page, or want to read it from page one again. So what am i missing on the last else if statement?
var linkedPage : Texture2D;
var linkedPage2 : Texture2D;
var linkedPage3 : Texture2D;
var linkedPage4 : Texture2D;
var LoadedLvL : int;
function OnGUI(){
if(Input.GetKeyDown("enter")){
///print("Enter Pressed");
Application.LoadLevel(LoadedLvL);
}
if(Input.GetKeyDown("space")){
///print("Space Pressed");
if (!audio.isPlaying){
audio.Play();
}
if(linkedPage != linkedPage2){
linkedPage = linkedPage2;
} else if(linkedPage2 != linkedPage3){
linkedPage2 = linkedPage3;
} else if(linkedPage3 != linkedPage4){
linkedPage3 = linkedPage4;
} else if(!linkedPage4){
linkedPage4 = linkedPage;
} else {
linkedPage4 = linkedPage;
}
}
var NextPage = new Rect((Screen.width * 0.15),(Screen.height * 0), 500, 120);
GUI.Label(NextPage, "Press (Space) To Read Next Page");
GUI.DrawTexture(new Rect(10,15,650,700),linkedPage);
}
If there is a better way to do this i would like to know, so i can improve it, thanks.
I’ve been trying to understand the reasoning behind your if…else if statement, so far with no success. Two things I really can’t understand are:
- if (linkedPage4 != linkedPage4) ALWAYS returns false, so why bother?
- linkedPage = linkedPage has absolutely NO effect in any reference-based language (such as C#), so again, why bother?
Besides, you’re messing the references up so many times that near the end, all initially referenced objects are unloaded anyway. I’m guessing you’re not familiar with C#'s reference system, so your first step should be to learn C# and its way of handling objects. The code you’re trying to write can be written like this (in C#, since I don’t know unityscript and I have no intentions of learning it any time soon):
public Texture2D linkedPages[];
int pageIndex;
void OnGUI(){
if (Input.GetKeyDown("space"))
pageIndex = (pageIndex + 1) % linkedPages.Length;
GUI.DrawTexture(... , linkedPages[pageIndex]);
}
@Arshia001 has already provided a solution using a Texture2D[]
array and an index. I recommend this approach as it is the most scalable i.e. another image can be added to the array without any changes to code.
However it looks like you are reverting back to the code in the original question. One of the reasons your original code was not working was that your were reassigning the linkedPage*
variables during runtime. This was getting them confused and restricting you from repeating the sequence.
If you want to use a variant on your original approach I suggest something like this:
var linkedPage1 : Texture2D;
var linkedPage2 : Texture2D;
var linkedPage3 : Texture2D;
var linkedPage4 : Texture2D;
private var currentPage : Texture2D;
function Start() {
currentPage = linkedPage1;
}
function Update() {
if(Input.GetKeyDown("space")){
if(currentPage == linkedPage1) {
currentPage = linkedPage2;
}
else if(currentPage == linkedPage2) {
currentPage = linkedPage3;
}
else if(currentPage == linkedPage3) {
currentPage = linkedPage4;
}
else if(currentPage == linkedPage4) {
currentPage = linkedPage1;
}
}
}
function OnGUI(){
//draw currentPage
}