How to declare and access other scripts publicly in Csharpe?

How can I access other scripts publicly in Csharpe?

I want to be able to reference a script from another script and then cache that so that I am able to just call it up whenever.

How would I declare this instance and cache it and use it in the update function?

I have already tried

void Start ()
{
RandomScript randomScript = cube.GetComponent<RandomScript>();
}

However I get an error saying randomScript cannot be found whenever I implement it in update. I know this is where I fall down because I just don’t know how to declare a script and this is nowhere in the docs other than scriptableObject and thts not what I want.

This is driving me nuts can anyone help me out here plz?

Make sure your “RandomScript” is a public class.

public class RandomScript : MonoBehaviour {

Make sure you add the script as a component to the cube.

Make sure your reference to the cube is correct.

If you do all that, your script should work.

I finally found the answer to this.

So to declare an instance of the script you want to access seems to be as follows

 //declare the actual script and then the name you want to 
 //reference it as - usually just make the first letter lower or 
 //upper case in order to keep it clean.
 public ScriptIWantToAccess scriptIWantToAccess;

Then within the “Start” function write the “referenced name” of the script not the actual script first and then use GetComponent to get the actual script as follows…

scriptIWantToAccess = cube.GetComponent<ScriptIWantToAccess >();

or

scriptIWantToAccess = cube.gameObject.GetComponent<ScriptIWantToAccess >();

Either should be fine however if your putting it on a camera I think it’s your camera object then same as up top otherwise it’s “.main.gameObject” and the rest as follows in the example for the GetComponent part.

Then finally you just call the instance name you made for the original script in update function or fixed update, OnGui etc and it should give you access to all the original scripts goodies

as follows…

scriptIWantToAccess.celebrationBanquetAchieved = true;

Hopefully this saves other people from considering headbutting a wall in frustration at this hurdle.

@melonman All of the above should be valid. Also make sure you are including the namespace to the script title, if there is one.

namespace UnityStandardAssets.Characters.FirstPersonController
{
    public class FirstPersonController : MonoBehaviour

So in order to call “FirstPersonController” script, we need to declare it’s namespace with either of these ways:

using UnityStandardAssets.Characters.FirstPerson;

or straight at the declaration of our variable

public UnityStandardAssets.Characters.FirstPerson.FirstPersonController player;