I write script several day ago, now … when i open Unity , all i get from the script is error…
dunno what kinda matter happened, but if it turn i’ll get 0 from 20 score of this lesson :((
Can’t read the screen shot of the script, use code blocks if you want someone to review it.
Yeah, the code is sort of legible in the image, but some of it’s cut off (or appears to be), so it’d be easier if you posted it using ‘code’ tags.
/* ///////////////////////////////////////////////////
//// Q.A.GUI ////
//// ////
//// This script define the way Question ////
//// And Answer GUIs should be shown. ////
//// ////
///////////////////////////////////////////////////
*/
//Variables
//SerializedVariable
//GUIStyles&Texture
var gSkin:GUISkin;
var gSkinEmpty:GUISkin;
var backGround :Texture2D;
var qMark :Texture2D;
var aMark :Texture2D;
//GUISize
var markSizeWidth:int=12;
var markSizeHeight:int=24;
//GUILocationSettings
var sideOffset:int=65;
var questionListLinesOffset:int=25;
var distanceBetweenQA:int=50;
var answerListLinesOffset:int=25;
//Question&AnswerDefinition
var questions:QuestionAnswer[]=new QuestionAnswer[1];
//Non-SerializedVariable
//ScreenSize
private var oldScreenWidthSize:int;
private var oldScreenHeightSize:int;
//GUILocation
private var nextQuestionOffset=0;
private var nextAnswerOffset=0;
//GUI-QuestionPageToShow
private var indexQuestionPage=-1;
//Class Variable
class QuestionAnswer
{
public var Question:Texture2D;
public var currectAnswerIndex:int=0;
public var Answers:Texture2D[]=new Texture2D[4];
function checking(){if (!Answers[currectAnswerIndex]) print("your pre setted answer is null");}
}
//DoPre-requiredSetting
function Awake()
{
if(!gSkin)
Debug.Log("StartMenuGUI: GUI Skin Object Missing !");
if(!gSkinEmpty)
Debug.Log("StartMenuGUI: GUI Skin Empty Object Missing !");
if(!qMark)
Debug.Log("StartMenuGUI: Question Mark Object Missing !");
if(!aMark)
Debug.Log("StartMenuGUI: Answer Mark Object Missing !");
//Reset GUI Position and depth by vectors to zero (we use pixel to set setting)
transform.position=Vector3(0,0,5);
//Hide GUI on Begining
this.GetComponent("GUITexture").enabled=false;
//set old Screen Width and Height to check for Reset() later
oldScreenWidthSize=Screen.width;
oldScreenHeightSize=Screen.height;
//call for reset with 'true' it will work any way
ResetGUIOnScreen(true);
}
//DoGUI
function OnGUI ()
{
//If state is Total Freeze : we are inside Station
if(GlobalStates.GAME_STATE==GameStates.ControllerTotalFreeze)
{
ResetGUIOnScreen(false);
if (this.GetComponent("GUITexture").enabled!=true)
{
this.GetComponent("GUITexture").enabled=true;
indexQuestionPage=-1;
}
DrawButtons();
DrawQuestions();
}
else
{
if (this.GetComponent("GUITexture").enabled!=false)
{
this.GetComponent("GUITexture").enabled=false;
}
}
}
//ResetPositionOfOurGUIDependOnScreenSize
function ResetGUIOnScreen(doAnyWay:boolean)
{
if((Screen.width!=oldScreenWidthSize || Screen.height!=oldScreenHeightSize) || doAnyWay)
{
if(this.GetComponent("GUITexture").pixelInset.x!=0)
{
this.GetComponent("GUITexture").pixelInset.x=0;
}
if(this.GetComponent("GUITexture").pixelInset.y!=
(Screen.height>this.GetComponent("GUITexture").pixelInset.height)?Screen.height/2-(this.GetComponent("GUITexture").pixelInset.height/2):0)
{
this.GetComponent("GUITexture").pixelInset.y=
(Screen.height>this.GetComponent("GUITexture").pixelInset.height)?Screen.height/2-(this.GetComponent("GUITexture").pixelInset.height/2):0;
}
}
}
//mainFormButtons
function DrawButtons()
{
GUI.skin.button=gSkin.customStyles[0];
if (GUI.Button(Rect(this.GetComponent("GUITexture").pixelInset.x+this.GetComponent("GUITexture").pixelInset.width-90,this.GetComponent("GUITexture").pixelInset.y+this.GetComponent("GUITexture").pixelInset.height-28,80,23),""))
{
GlobalStates.GAME_STATE=GameStates.Running;
}
GUI.skin.button=gSkin.customStyles[1];
if (GUI.Button(Rect(this.GetComponent("GUITexture").pixelInset.x+this.GetComponent("GUITexture").pixelInset.width-32,this.GetComponent("GUITexture").pixelInset.y+19,30,22),""))
{
GlobalStates.GAME_STATE=GameStates.Running;
}
}
//QuestionAndAnswers
function DrawQuestions()
{
GUI.skin=gSkinEmpty;
var i=0;
if (indexQuestionPage==-1)
{
for(i=0;i<questions.Length;i++)
{
if (questions[i].Question)
{
nextQuestionOffset=i*questionListLinesOffset;
if(GUI.Button(Rect(this.GetComponent("GUITexture").pixelInset.x+this.GetComponent("GUITexture").pixelInset.width-sideOffset,this.GetComponent("GUITexture").pixelInset.y+120+nextQuestionOffset,markSizeWidth,markSizeHeight),qMark))
{
indexQuestionPage=i;
}
if(GUI.Button(Rect(this.GetComponent("GUITexture").pixelInset.x+this.GetComponent("GUITexture").pixelInset.width-sideOffset-(markSizeWidth+questions[i].Question.width),this.GetComponent("GUITexture").pixelInset.y+120+nextQuestionOffset,questions[i].Question.width,questions[i].Question.height),questions[i].Question))
{
indexQuestionPage=i;
}
}
}
}
else
{
nextAnswerOffset=questions[indexQuestionPage].Question.height;
GUI.Label(Rect(this.GetComponent("GUITexture").pixelInset.x+this.GetComponent("GUITexture").pixelInset.width-sideOffset,this.GetComponent("GUITexture").pixelInset.y+120,markSizeWidth,markSizeHeight),qMark);
GUI.Label(Rect(this.GetComponent("GUITexture").pixelInset.x+this.GetComponent("GUITexture").pixelInset.width-sideOffset-(markSizeWidth+questions[indexQuestionPage].Question.width),this.GetComponent("GUITexture").pixelInset.y+120,questions[indexQuestionPage].Question.width,questions[indexQuestionPage].Question.height),questions[indexQuestionPage].Question);
nextAnswerOffset+=distanceBetweenQA;
for(i=0;i<questions[indexQuestionPage].Answers.Length;i++)
{
if (questions[indexQuestionPage].Answers[i])
{
if(GUI.Button(Rect(this.GetComponent("GUITexture").pixelInset.x+this.GetComponent("GUITexture").pixelInset.width-sideOffset,this.GetComponent("GUITexture").pixelInset.y+120+nextAnswerOffset,markSizeWidth,markSizeHeight),aMark))
{
//check asnwer...
//apply scores...
//back to first page
}
if(GUI.Button(Rect(this.GetComponent("GUITexture").pixelInset.x+this.GetComponent("GUITexture").pixelInset.width-sideOffset-(markSizeWidth+questions[indexQuestionPage].Answers[i].width),this.GetComponent("GUITexture").pixelInset.y+120+nextAnswerOffset,questions[indexQuestionPage].Answers[i].width,questions[indexQuestionPage].Answers[i].height),questions[indexQuestionPage].Answers[i]))
{
//same as top
//check asnwer...
//apply scores...
//back to first page
}
nextAnswerOffset+=answerListLinesOffset+questions[indexQuestionPage].Answers[i].height;
}
}
}
}
hey i post the codes…
Now, apply the camera to myObject in the hierarchy.
/* ///////////////////////////////////////////////////
//// Q.A.GUI ////
//// ////
//// myObject script define the way Question ////
//// And Answer GUIs should be shown. ////
//// ////
///////////////////////////////////////////////////
*/
//Variables
//SerializedVariable
//GUIStyles&Texture
public var myObject;
var gSkin:GUISkin;
var gSkinEmpty:GUISkin;
var backGround :Texture2D;
var qMark :Texture2D;
var aMark :Texture2D;
//GUISize
var markSizeWidth:int=12;
var markSizeHeight:int=24;
//GUILocationSettings
var sideOffset:int=65;
var questionListLinesOffset:int=25;
var distanceBetweenQA:int=50;
var answerListLinesOffset:int=25;
//Question&AnswerDefinition
var questions:QuestionAnswer[]=new QuestionAnswer[1];
//Non-SerializedVariable
//ScreenSize
private var oldScreenWidthSize:int;
private var oldScreenHeightSize:int;
//GUILocation
private var nextQuestionOffset=0;
private var nextAnswerOffset=0;
//GUI-QuestionPageToShow
private var indexQuestionPage=-1;
//Class Variable
class QuestionAnswer
{
public var Question:Texture2D;
public var currectAnswerIndex:int=0;
public var Answers:Texture2D[]=new Texture2D[4];
function checking(){if (!Answers[currectAnswerIndex]) print("your pre setted answer is null");}
}
//DoPre-requiredSetting
function Awake()
{
if(!gSkin)
Debug.Log("StartMenuGUI: GUI Skin Object Missing !");
if(!gSkinEmpty)
Debug.Log("StartMenuGUI: GUI Skin Empty Object Missing !");
if(!qMark)
Debug.Log("StartMenuGUI: Question Mark Object Missing !");
if(!aMark)
Debug.Log("StartMenuGUI: Answer Mark Object Missing !");
//Reset GUI Position and depth by vectors to zero (we use pixel to set setting)
myObject.transform.position=Vector3(0,0,5);
//Hide GUI on Begining
myObject.GetComponent("GUITexture").enabled=false;
//set old Screen Width and Height to check for Reset() later
oldScreenWidthSize=Screen.width;
oldScreenHeightSize=Screen.height;
//call for reset with 'true' it will work any way
ResetGUIOnScreen(true);
}
//DoGUI
function OnGUI ()
{
//If state is Total Freeze : we are inside Station
if(GlobalStates.GAME_STATE==GameStates.ControllerTotalFreeze)
{
ResetGUIOnScreen(false);
if (myObject.GetComponent("GUITexture").enabled!=true)
{
myObject.GetComponent("GUITexture").enabled=true;
indexQuestionPage=-1;
}
DrawButtons();
DrawQuestions();
}
else
{
if (myObject.GetComponent("GUITexture").enabled!=false)
{
myObject.GetComponent("GUITexture").enabled=false;
}
}
}
//ResetPositionOfOurGUIDependOnScreenSize
function ResetGUIOnScreen(doAnyWay:boolean)
{
if((Screen.width!=oldScreenWidthSize || Screen.height!=oldScreenHeightSize) || doAnyWay)
{
if(myObject.GetComponent("GUITexture").pixelInset.x!=0)
{
myObject.GetComponent("GUITexture").pixelInset.x=0;
}
if(myObject.GetComponent("GUITexture").pixelInset.y!=
(Screen.height>myObject.GetComponent("GUITexture").pixelInset.height)?Screen.height/2-(myObject.GetComponent("GUITexture").pixelInset.height/2):0)
{
myObject.GetComponent("GUITexture").pixelInset.y=
(Screen.height>myObject.GetComponent("GUITexture").pixelInset.height)?Screen.height/2-(myObject.GetComponent("GUITexture").pixelInset.height/2):0;
}
}
}
//mainFormButtons
function DrawButtons()
{
GUI.skin.button=gSkin.customStyles[0];
if (GUI.Button(Rect(myObject.GetComponent("GUITexture").pixelInset.x+myObject.GetComponent("GUITexture").pixelInset.width-90,myObject.GetComponent("GUITexture").pixelInset.y+myObject.GetComponent("GUITexture").pixelInset.height-28,80,23),""))
{
GlobalStates.GAME_STATE=GameStates.Running;
}
GUI.skin.button=gSkin.customStyles[1];
if (GUI.Button(Rect(myObject.GetComponent("GUITexture").pixelInset.x+myObject.GetComponent("GUITexture").pixelInset.width-32,myObject.GetComponent("GUITexture").pixelInset.y+19,30,22),""))
{
GlobalStates.GAME_STATE=GameStates.Running;
}
}
//QuestionAndAnswers
function DrawQuestions()
{
GUI.skin=gSkinEmpty;
var i=0;
if (indexQuestionPage==-1)
{
for(i=0;i<questions.Length;i++)
{
if (questions[i].Question)
{
nextQuestionOffset=i*questionListLinesOffset;
if(GUI.Button(Rect(myObject.GetComponent("GUITexture").pixelInset.x+myObject.GetComponent("GUITexture").pixelInset.width-sideOffset,myObject.GetComponent("GUITexture").pixelInset.y+120+nextQuestionOffset,markSizeWidth,markSizeHeight),qMark))
{
indexQuestionPage=i;
}
if(GUI.Button(Rect(myObject.GetComponent("GUITexture").pixelInset.x+myObject.GetComponent("GUITexture").pixelInset.width-sideOffset-(markSizeWidth+questions[i].Question.width),myObject.GetComponent("GUITexture").pixelInset.y+120+nextQuestionOffset,questions[i].Question.width,questions[i].Question.height),questions[i].Question))
{
indexQuestionPage=i;
}
}
}
}
else
{
nextAnswerOffset=questions[indexQuestionPage].Question.height;
GUI.Label(Rect(myObject.GetComponent("GUITexture").pixelInset.x+myObject.GetComponent("GUITexture").pixelInset.width-sideOffset,myObject.GetComponent("GUITexture").pixelInset.y+120,markSizeWidth,markSizeHeight),qMark);
GUI.Label(Rect(myObject.GetComponent("GUITexture").pixelInset.x+myObject.GetComponent("GUITexture").pixelInset.width-sideOffset-(markSizeWidth+questions[indexQuestionPage].Question.width),myObject.GetComponent("GUITexture").pixelInset.y+120,questions[indexQuestionPage].Question.width,questions[indexQuestionPage].Question.height),questions[indexQuestionPage].Question);
nextAnswerOffset+=distanceBetweenQA;
for(i=0;i<questions[indexQuestionPage].Answers.Length;i++)
{
if (questions[indexQuestionPage].Answers[i])
{
if(GUI.Button(Rect(myObject.GetComponent("GUITexture").pixelInset.x+myObject.GetComponent("GUITexture").pixelInset.width-sideOffset,myObject.GetComponent("GUITexture").pixelInset.y+120+nextAnswerOffset,markSizeWidth,markSizeHeight),aMark))
{
//check asnwer...
//apply scores...
//back to first page
}
if(GUI.Button(Rect(myObject.GetComponent("GUITexture").pixelInset.x+myObject.GetComponent("GUITexture").pixelInset.width-sideOffset-(markSizeWidth+questions[indexQuestionPage].Answers[i].width),myObject.GetComponent("GUITexture").pixelInset.y+120+nextAnswerOffset,questions[indexQuestionPage].Answers[i].width,questions[indexQuestionPage].Answers[i].height),questions[indexQuestionPage].Answers[i]))
{
//same as top
//check asnwer...
//apply scores...
//back to first page
}
nextAnswerOffset+=answerListLinesOffset+questions[indexQuestionPage].Answers[i].height;
}
}
}
}
so u apply Script on 1 object that here is texture2D (but u used camera)… and again pass that object to the Object variable which u define to fix it?!
So basically, here is what it boils down to, when I applied the script to a game object that you wrote, I got the same error, the problem is “this” was not pointing to anything that was a game object even though the java script is on a game object. So to fix that, it doesn’t matter what game object the code sits on, but have a public variable and give that the game object, in this case, I used the camera, now all of the script works, you can use anything you want or need, based on what the code is meant to do, I just got rid of the error, but I do not have the full code because there are other components to this code to make it work that you have, so you will have to play with it to make it work, in this case, just assign the texture2D instead of the camera to the variable and have the script on the camera or other game object, should work just fine. The code is setup to accept anything as the target, in your case, the texture2D.
tnx, but i randomly change my object name, and all error gone…
it seem was that i changed script name before close unity several days ago, and that’s cuzed that i dont notice that…
But the way u say is correct too… tnx for time…
it seem, it pointed to my class “AnswerQuestion” instead of game object it self, while game object name is same as class inside it…



