Using the same int on different scripts

heya guys,

So I know that this has been posted many times. But I couldn’t really understand using the ‘getcomponent’ etc.
I want to use a variable called ‘aantal’ to make you only pick up one weapon.
The script I’ve made so far:

#pragma strict

var scimitaroppakken : boolean;
public var aantal : int;

function OnTriggerEnter (other : Collider)

{
    if(other.tag == "Player")
   
    {
        scimitaroppakken = true;   
    }

}

function OnTriggerExit (other : Collider)

{
    if(other.tag == "Player")
   
    {
        scimitaroppakken = false;   
    }
       
}


function Update ()

{
    if(scimitaroppakken == true)
   
    {
        if(Input.GetMouseButton(0))
       
        {
       
            if(aantal < 1)
            {
       
                 gameObject.active = false;
                 aantal = aantal + 1;
            }
        }
    }
}

and script 2:

#pragma strict

var zwaardoppakken : boolean;
public var aantal : int;

function OnTriggerEnter (other : Collider)

{
    if(other.tag == "Player")
   
    {
        zwaardoppakken = true;   
    }

}

function OnTriggerExit (other : Collider)

{
    if(other.tag == "Player")
   
    {
        zwaardoppakken = false;   
    }
       
}


function Update ()

{
    if(zwaardoppakken == true)
   
    {
   
        if(Input.GetMouseButton(0))
       
        {
            if(aantal < 1)
            {
       
                 gameObject.active = false;
                 aantal = aantal + 1;
            }
        }
    }
}

I dont understand why the public var aantal is not working on the other script

Check out my signature for a get component tutorial that will help you out.

Best,

Jon

Will do :slight_smile:
But I see you are working with C# and I am using javascript(unityscript)

anybody else?

It’s the same for javascript and c#. It’s the Unity API.

That video tells you exactly what you need todo. I hope nobody just spoon feeds you the answer.

Best of luck,

Jon

I cant understand WHAT you expect this script to do but if you want the variable to only have ONE instance across several copies you need to look into the static keyword. Ill not say more because it is a pretty basic programming concept and easily googled.