- Home /
GUI Text from script Java
Hi I am trying to get my randomly generated to names to appear on some GUI in game. For this it would be the Name1. I have tryed as seen below in my code. (Nameone is the name of the gui in game,)
#pragma strict
#pragma strict
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var Name1Choices : String[] = ["Bob", "Mary", "Maxo"];
var Name2Choices : String[] = ["Berry", "Richards", "Stephens"];
var Backstory1Choices : String[];
var Backstory2Choices : String[];
var Backstory3Choices : String[];
var Name1;
var Name2;
var Backstory1;
var Backstory2;
var Backstory3;
var mission : boolean = false;
var money : float;
function Start ()
{
MissionGen();
}
function MissionGen ()
{
if( !mission )
{
var pos : Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)];
Instantiate(obj, pos.position, pos.rotation);
Name1 = Name1Choices[ Random.Range( 0, Name1Choices.length ) ]; //Randomly Generated Name
Name2 = Name2Choices[ Random.Range( 0, Name2Choices.length ) ]; //Randomly Generated Name Part 2
Backstory1 = Backstory1Choices[ Random.Range( 0, Backstory1Choices.length ) ]; //Randomly Generated BackStory Part 1
Backstory2 = Backstory2Choices[ Random.Range( 0, Backstory2Choices.length ) ]; //Randomly Generated Backstroy Part 2
Backstory3 = Backstory3Choices[ Random.Range( 0, Backstory3Choices.length ) ]; //Randomly Generated Backstory Part 3
mission = true;
// GameObject.Find("Nameone").guiText.text = Name1; my feeble attempt at getting the gui to work
}
else
{
Invoke( "MissionGen", 30.0f );
}
}
function Check ()
{
var BadGuyCheck : GameObject = GameObject.FindGameObjectWithTag( "BadGuy" );
if ( BadGuyCheck == null )
{
mission = false;
money += Random.Range(1000,10000);
yield WaitForSeconds (30);
Invoke( "MissionGen", 30.0f );
}
}
Try this //Declare at the top
var guiText : GUIText;
And replace
// GameObject.Find("Nameone").guiText.text = Name1;
with
guiText.text = Name1;
I now get this error pop up any help?
Assets/Scripts/$$anonymous$$ission.js(41,9): BCE0004: Ambiguous reference 'guiText': $$anonymous$$ission.guiText, UnityEngine.Component.guiText.
haha never$$anonymous$$d, the comment i posted earlier was actually bad, and the reason you get the error is my fault.
"guiText" is already a variable used by GameObject to refer to the guiText component. So change the name of you GUIText and it should work
so at the start do
var ExampleName : GUIText; //choose anything but guiText
then on line 41
ExampleName.text = Name1;
Answer by El Maxo · Mar 22, 2014 at 11:55 AM
#pragma strict
#pragma strict
var enemyPrefabs : GameObject[]; // Array of different Enemies that are used.
var spawnPoints : Transform[]; // Array of spawn points to be used.
var Name1Choices : String[] = ["Bob", "Mary", "Maxo","Phil","Noobas","Hervard","Dan","Daniel","Johnathan"];
var Name2Choices : String[] = ["Berry", "Richards", "Stephens"];
var Backstory1Choices : String[];
var Backstory2Choices : String[];
var Backstory3Choices : String[];
var Name1;
var Name2;
var Backstory1;
var Backstory2;
var Backstory3;
var guiText : GUIText;
var mission : boolean = false;
var money : float;
function Start ()
{
MissionGen();
}
function MissionGen ()
{
if( !mission )
{
var pos : Transform = spawnPoints[Random.Range(0, spawnPoints.length)]; // Randomize the spawnPoints to instantiate enemy at next.
var obj : GameObject = enemyPrefabs[Random.Range(0, enemyPrefabs.length)];
Instantiate(obj, pos.position, pos.rotation);
Name1 = Name1Choices[ Random.Range( 0, Name1Choices.length ) ]; //Randomly Generated Name
Name2 = Name2Choices[ Random.Range( 0, Name2Choices.length ) ]; //Randomly Generated Name Part 2
Backstory1 = Backstory1Choices[ Random.Range( 0, Backstory1Choices.length ) ]; //Randomly Generated BackStory Part 1
Backstory2 = Backstory2Choices[ Random.Range( 0, Backstory2Choices.length ) ]; //Randomly Generated Backstroy Part 2
Backstory3 = Backstory3Choices[ Random.Range( 0, Backstory3Choices.length ) ]; //Randomly Generated Backstory Part 3
mission = true;
guiText.text = Name1;
}
else
{
Invoke( "MissionGen", 30.0f );
}
}
function Check ()
{
var BadGuyCheck : GameObject = GameObject.FindGameObjectWithTag( "BadGuy" );
if ( BadGuyCheck == null )
{
mission = false;
money += Random.Range(1000,10000);
yield WaitForSeconds (30);
Invoke( "MissionGen", 30.0f );
}
}
Your answer
Follow this Question
Related Questions
How to make GUI Text appear after a certain amount of time 2 Answers
Create GUIText from Javascript 3 Answers
How to change the text on an other object ? 2 Answers
Changing 3d text through script 1 Answer