- Home /
Something wrong with this script?
I got an error says : Assets/ActiveGenerator.js(10,66): BCE0049: Expression 'self.GeneratorGUI.GetComponent(GeneratorCountNumbers).CountGenerator' cannot be assigned to.
These are my scripts. Script A : This script called GeneratorCountNumbers :
//Script A
var CountGenerator : int = 0;
function OnGUI()
{
if(CountGenerator == 1)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 2 more generator to activate the gate");
}
if(CountGenerator == 2)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 1 more generator to activate the gate");
}
if(CountGenerator == 3)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Gate is now activated");
}
}
Script B : This script called ActiveGenerator
//Script B
var message : boolean = false;
var GeneratorGUI = GameObject;
function OnTriggerStay(other : Collider)
{
if(Input.GetButton("e"))
{
GeneratorGUI.GetComponent(GeneratorCountNumbers).CountGenerator++;
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
message = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
message = false;
}
}
function OnGUI(){
if(message){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to activate generator");
}
}
Errors at GetComponent in Script B Gameobject that attached with Script A called "GeneratorGUI"
Answer by Bunny83 · Sep 22, 2013 at 03:21 PM
This line:
var GeneratorGUI = GameObject;
does not declare a variable of type GameObject but a variable of type System.Type which holds the type information of the GameObject class.
You need to declare it like this:
var GeneratorGUI : GameObject;
And you need to assign a gameobject to the variable before you use it.
Oh god... are you kidding me? ... That's why my script getting error! Thanks..
I'm too stupid. I didn't notice that xD
Answer by Unique-Games · Sep 22, 2013 at 03:08 PM
I've ever used .GetComponent(), so i wont be able to help as much as i would like but what i do instead is in ScriptA have your variable as a static var (I.e. static var CountGenerator : int = 0;), then in ScriptB instead of GeneratorGUI... use ScriptA.CountGenerator++; (Replace ScriptA with the name of your first script (which contains CountGenerator). Hope this helps :) - UniqueGames
Is it gonna be like this
SCRIPT A
//Script A
static var CountGenerator : int = 0;
function OnGUI()
{
if(CountGenerator == 1)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 2 more generator to activate the gate");
}
if(CountGenerator == 2)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 1 more generator to activate the gate");
}
if(CountGenerator == 3)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Gate is now activated");
}
}
and SCRIPT B
//Script B
var message : boolean = false;
var GeneratorGUI = GameObject;
function OnTriggerStay(other : Collider)
{
if(Input.GetButton("e"))
{
GeneratorGUI.GetComponent(ScriptA).CountGenerator++;
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
message = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
message = false;
}
}
function OnGUI(){
if(message){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to activate generator");
}
}
It gives me an error
Assets/ActiveGenerator.js(10,18): BCE0005: $$anonymous$$ identifier: 'ScriptA'.
Answer by YoungDeveloper · Sep 22, 2013 at 03:14 PM
//Script B
var message : boolean = false;
var thatObject = GameObject;
var script_A : ScriptA;
function OnTriggerStay(other : Collider)
{
if(Input.GetButton("e"))
{
script_A = thatObject.GetComponent("GeneratorGUI");
script_A.Action();
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
message = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
message = false;
}
}
function OnGUI(){
if(message){
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Press E to activate generator");
}
}
//////////////////////////////////
//Script A
var CountGenerator : int = 0;
function Action(){
CountGenerator++;
}
function OnGUI()
{
if(CountGenerator == 1)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 2 more generator to activate the gate");
}
if(CountGenerator == 2)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "You need 1 more generator to activate the gate");
}
if(CountGenerator == 3)
{
GUI.Label(new Rect(Screen.width/2 - 75, Screen.height - 100, 300, 30), "Gate is now activated");
}
}
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
How to import the object from server to unity 2 Answers
Can someone help me fix my Javascript for Flickering Light? 6 Answers
Material doesn't have a color property '_Color' 4 Answers
Setting Scroll View Width GUILayout 1 Answer