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”

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.

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 :slight_smile: - UniqueGames

//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");
}
}