Accessing vars/functions of other scripts

Trying to follow the site tutorials, but could use a more well written tutorial then otherScript OtherScript so been trying to do it solo and no matter how I try it, seems to crash and burn.
so on my main player i have a c# script, and in it I have this, but i am mostley pointing/questioning on the score and the addScore function,

using UnityEngine;
using System.Collections;

public class playercontrols : MonoBehaviour {
   
    public float  playerSpeed = 15;
    public float  maxWidth = 25;
    public float  maxHeight = 4;
    public int maxShots = 5;
   
    public int lives = 3;
    public int score = 0;
    public int level = 0;
    public int powerup = 0;
    public float powerupLeft = 0;
    public GameObject bullet = null;
    public Transform bulletSpawn = null;
   
    private float playerCurX = 0;
    private float playerCurY = 0;
    void Start () 
    {
        transform.position = new Vector3(playerCurX, 0, 0);
    }
   
    void Update () 
    {
        //See if Player is Moving - change to newtranslations if so
        float newTranslationX = Input.GetAxis("Horizontal") * playerSpeed;
        newTranslationX *= Time.deltaTime;
        playerCurX = playerCurX + newTranslationX;
        float newTranslationY = Input.GetAxis("Vertical") * playerSpeed;
        newTranslationY *= Time.deltaTime;
        playerCurY = playerCurY + newTranslationY;
        //Check NEW Translations with max range, if moving outside range set to max
        if(playerCurY >= maxHeight)
        { playerCurY = maxHeight; }
        if(playerCurY <= -maxHeight)
        { playerCurY = -maxHeight; }
        if(playerCurX >= maxWidth)
        { playerCurX = maxWidth; }
        if(playerCurX <= -maxWidth)
        { playerCurX = -maxWidth; }
        //Set it
        transform.position = new Vector3(playerCurX, 0, playerCurY);
       
        //Check to see if shooting
        if(Input.GetButtonDown("Jump"))
        { 
           
            Instantiate(bullet, bulletSpawn.position, bulletSpawn.rotation); 
        }
       

    }
   
   
    static void addScore(int adder)
    {
        score += adder;
    }
}

now, on the other objects, im setting it to when they are destroyed, to add a value to the players score, and then in another script remove a object so it will spawn a new one.

the player score function is

    static void addScore(int adder)
    {
        score += adder;
    }

The spawner script has a function -

    static void subObject()
    {
        curObjects -= 1;
    }

When the target is destroyed, its code is

        if(transform.position.y <= -killRange)
        {
            playerScript = GetComponent("playercontrols") as playercontrols;
            playerScript.addScore(10);
            spawnscript = GetComponent("spawner") as spawner;
            spawnscript.subObject();
            Destroy(gameObject);
        }

The playercontrols.cs script is attached to the main player, and the script spawner.cs is attached to a empty game object that handels the random spawning.

With this, Unity will either return no errors, but then on running it throw tons of null refrence errors,
or will error with any one of the following…
Assets/Scripts/enemyShip.cs(26,38): error CS0122: `playercontrols.addScore(int)’ is inaccessible due to its protection level

cannot be accessed with an instance reference, qualify it with a type name instead
The error seems to change or just not be caught some times for some reason then just messes up in the game.
Any suggestions on the best way to access variables of other scripts?

Ok so I managed to get it working, but it raises a weird question.
If when I declared it as Static, makes it un-accessable to the other scripts, leavint the vars as just regular public name allows me to access them, then why are the tutorials saying use static vars and all these getcomponents and linking scripts?
Or guess what is the diffrence/best practice?
So now I can adjust the vars from other scripts, from just using . = new value;
Is this ok to leave as it is or does it raise problems/issues down the line and why the tutorials saying to access other vars to do all these weird things like this guide - http://docs.unity3d.com/412/Documentation/ScriptReference/index.Accessing_Other_Game_Objects.html
Sorry for prob low questions but I am trying to understand how it all works and even though now the problem works, I dont understand how so its not enough to make it work if dont know how it works so wont be able to know how to do it later or in other ways :slight_smile:

I see two issues with the example you provided, and I suspect it is both of these cropping up at the same time that might be throwing you off.

Your first issue, as described by the error “Assets/Scripts/enemyShip.cs(26,38): error CS0122: `playercontrols.addScore(int)’ is inaccessible due to its protection level”, is due to what we call Member Accessibility.

This…

static void addScore(int adder)
{
     score += adder;
}

should read as this…

public static void addScore(int adder)
{
     score += adder;
}

Public is an accessibility keyword which tells Unity that this member is accessible by classes that exist OUTSIDE of the scope of this particular class. By default we assume that members that don’t have access modifiers are private and as such are inaccessible from external classes.

The second issue is due to you trying to access a types static member through an instance of that type. Static members are associated with the type as a whole and not the individual objects (or instances) created from that type.

So this…

public static void addScore(int adder)
{
     score += adder;
}

should read as this…

public void addScore(int adder)
{
     score += adder;
}

A similar change should be made to your spawner class so that

public static void subObject()

reads as

public void subObject()

This change will tell Unity that these methods belong to particular instances of the object, and each method operates on that one single instance of the object it is associated with.

For more info you can check Microsofts MSDN documentation on Member Accessibility and the Static keyword.