Sharing Functions with c# without linking to Gameobjects

Ok, I have two scripts, one script is on a GameObject and the second script is NOT. But contains code that needs to be shared with the first script.

I’ve tried using Namespace, but apparently I’m doing something wrong.

First script attached to object:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using Panel;

public class Placement : MonoBehaviour
{
    public void OnButton1Click()
    {

        //How do I load text from PanelDisplay AlphaInfo fuction script???

    }
}

Second script not attached:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;

namespace Panel
{
    public class PanelDisplay : MonoBehaviour
    {
        public Text Text1;
        public Text Text2;
        public Text Text3;

        public void AlphaInfo()
        {
            this.Text1.text = "something 1";
            this.Text2.text = "something 2";
            this.Text3.text = "something 3";
        }

    }
}

Why is the second script inheriting from MonoBehaviour if it isnt on a game object?

You need to create a new instance of the second object in the first script OR make the method static.

I guess you are kinda new to object orientated programming?

Korno,

Correct, I’m still trying to learn the way certain things function and why. I did not know that if a script is not going on the game object, it no longer needs the MonoBehaviour inheritance. I’ve gone ahead and removed the MonoBehaviour from the second script, and trying to modify the first script to load the Text1,2,3 variables. This is what I have now for the first script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
using Panel;

public class Placement : MonoBehaviour
{
    public void OnButton1Click()
    {

      PanelDisplay panel = new PanelDisplay();
     panel.Text1 = ???

    }
}

Sorry for the rookie question, we all have to start somewhere right?

Nevermind, I think I fixed it with:

PanelDisplay panel = new PanelDisplay();
        panel.AlphaInfo();

Hey, sorry if I came across a bit rude. It wasnt my intention.

But yeah you have got it right. You could move the object creation out of the OnButtonClick as you are making a new instance every time the button is clicked. Maybe create the object in Start() or something and just use the object in OnButtonClick.