Change Variable from other script

So i was thinking , and i can’t find the solution (dumb)…
I wanted like when i trigger a box and press E the value from script 2 to add to value in script 1
So let’s see :
Script 1(Pick up script)

var script :  WeaponIndex;
var Index : int = 2;

function OnTriggerEnter (other : Collider) {
	if(other.gameObject.tag == "Weapons")
	{
		
		Triggered = true;
		Debug.Log("Weapon");
		ShowGUI = true;
		
	}
}
function Update() {
if (Triggered  Input.GetKey(KeyCode.E))
{
//add WpIndex from Script 2 to this script 1 
script = GetComponent("WeaponIndex");		
Index == script.WpIndex;

}

}

And Script 2 is simple :

var WpIndex : int = 2;

So real question is how to add value from Script 2 (WpIndex) to value in Script 1 (Index)?
Any help,tips?

Thank You…

make WpIndex public, like public var WpIndex : int = 2;

In this case the public isn’t required. uJavaScript global variables are public unless marked otherwise.

Index == script.WpIndex; should read as Index = script.WpIndex;
Your version is testing for equality between Index and WpIndex you want an assignment which is a single equal sign.

I only use C# so i get confused with unityscript.

Hi it was

i wrote wrong in here. But the script it was trying to access was named WeaponIndex 2 so i changed it to WeaponIndex .
But now when i press E i get error

function Update ()
{
if (Triggered  Input.GetKey(KeyCode.E))
{
script = GetComponent("WeaponIndex");	
//This is line 17 **************************	
Index = script.WpIndex;
}

}

I don’t know what to do to fix this

Can anybody help?

Unity mentions it cannot find an object, this (in the case of c# atleast, i dont use unityscript myself) this can likely be the result of either;
-The object the script is running from, does not contain a Component/script named WeaponIndex.
-You tried looking up an object using GetComponent instead of GameObject.Find(“ObjectName”)

Hope it’ll get you somewhere :slight_smile:

I’d agree with Annihlator, from the code it’s hard to tell but do you have the WeaponIndex.js script on the object that is the player or the object that is the gun?

The way you’ve written it in terms of the GetComponent search will only work if both scripts are on the same object but from the collision section it looks like you are actually using this to detect which weapon the player bumped into so I dunno… it’s not clear from your example.

I placed these two scripts on a single object and when I toggle the boolean ‘Triggered’ in the inspector then press the E key the value of Index changed from it’s default -1 to 2.

PickUp.js

#pragma strict

var script :  WeaponIndex;
var Index : int = -1;

//unspecified variables required for operational reference but not 
//included in OP's example code

var Triggered : boolean = false;
var ShowGUI : boolean = false;
     
function OnTriggerEnter (other : Collider) 
	{
    if(other.gameObject.tag == "Weapons")
        {
        Triggered = true;
		Debug.Log("Weapon");
		ShowGUI = true;           
        }
    }
    
function Update() 
	{
    if (Triggered  Input.GetKey(KeyCode.E))
    	{
    	//add WpIndex from Script 2 to this script 1
    	script = GetComponent("WeaponIndex");      
    	Index = script.WpIndex;     
	    }     
    }

and

WeaponIndex.js

#pragma strict
var WpIndex : int = 2;

I’m sorry i wasn’t at home lately.

But how can i change so i it can detect the WpIndex that’s on other object?
Since i want when i collide whit the weapon on ground to get that weapons number(Index)and with that number i can change weapon?

You could use

These should all give you some insight into what to do.

A quick example is

GameObject.Find(“NameOfGOWithScript”).GetComponent(ScriptName) (i think in UnityScript)