Scope and Access Modifiers

Script #1

using System.Collections;
using UnityEngine;

public class ScopeAndAccessModifiers : MonoBehaviour 
{
    public int alpha = 5;

    private int beta = 0;
    private int gamma = 5;

    private AnotherClass myOtherClass;

    void Start () 
    {
        alpha = 29;

        myOtherClass = new AnotherClass();
        myOtherClass.FruitMachine(alpha, myOtherClass.apples);
    }

    void Example (int pens, int crayons) 
    {
        int answer;
        answer = pens * crayons * alpha;
        Debug.Log(answer);
    }

    void Update () 
    {
        Debug.Log(“Alpha is set to: “ + alpha);
    }
}

Script #2

using System.Collections;
using UnityEngine;

public class AnotherClass : MonoBehaviour
{
    public int apples;
    public int bananas;

    private int stapler;
    private int sellotape;

    public void FruitMachine(int a, int b)
    {
        int answer;
        answer = a + b;
        Debug.Log(“Fruit total: “ + answer);
    }

    private void OfficeSort(int a, int b)
    {
        int answer;
        answer = a + b;
        Debug.Log(“Office Supplies total: + answer”);
    }
}

Screencast

What I Learned

In this tutorial the focus was to show you how to change scope and access modifiers within script. With the script attached to the object, you can then put intervals that show up in the inspector view so you can change the intervals with Unity instead of going back and changing the script. One problem I had to solve was that I didn’t realize I had an extra “s” in the title of my script, so an error popped up in Unity that my script couldn’t be used. This actually took me half the period to figure out because it was so tiny and I’m a little tired as I go through making these.

Leave a Reply

Your email address will not be published. Required fields are marked *