Accessing variable in another script

Lets start this with Unity is a good engine but sharing data between scripts is like pulling teeth. Especially if their not on the same object.

I have a script, PKDataGeneration, it has a variable in it, PKBase, I need that variable in my LightUp script.I’m using C# Now ordinarily I could include that at the top with Include PKDataGeneration or Using PKDataGeneration, but because this is unity it won’t let me do that. I’ve been through at least 20 different Unity Answers posts, and they either made my problem worse or didn’t make sense in the least. Can someone please just tell me what command(s) I need to access my PKDataGeneration script, get the PKBase variable, and return it to the LightUp script so I can change this spheres color.

If I know pre build, pre run, what script needs to access what script I use…

public someScript someScriptAccessor;

/////then when I need to call it....
void someFunction(){
someScriptAccessor.doThis();
someScriptAccessor.someValue = thisThat;
}

If I dont know pre hand, I use getComponent or sendMessage. Also, Find() to get a gameObject if it must be found.

Um…Huh?

I have absolutely no idea what you just referenced. are you saying I should have this in it:

int lit=PKDataGenerator.PKBase

Because I havn’t been able to get that to work

What gameobject is PKDataGeneration attached to?

breadboard, which is just a name I gave the plane I put in, I’d rather PKDataGeneration just be floating but I don’t think unity likes that

You can have scripts floating around if you just have an empty game object in the scene and attach all your floating scripts to that.

Anyway make sure that PKBase is public then in the LightUp script:

public GameObject whatever; // drag the breadboard game object into this in the inspector

then in Update()

PKDataGeneration pkdg = whatever.GetComponent<PKDataGeneration>();
something = pkgd.PKBase; // put the value of PKBase into whatever you need

A common misconception among beginners is that every class needs to inherit from MonoBehaviour. Sounds like this might be part of your problem.

Also - “using”’ statements are only used for exposing sets of classes inside other namespaces. Putting “using SomeClass” at the top of your code won’t expose data in instances of SomeClass.

THanks, that helped alot