Help a n00b

My programing skills are very dated but I am usually able to figure this stuff out.

Can someone please explain to me how to reference code from other scripts.

I know this is very basic stuff but I can’t seem to get it to work.

I have tried to make the GameObject from the script where I am trying to get access thinking that might be it but to no avail.

In fact along with how this is done… I had assumed that getting something from another script would be as simple as something like

GetComponent.Barrel.Field_Direction

Where Barrel is some other script and within I have something like

public string[ ] Field_Direction = {“North”, “East”, “South”, “West”}

Part of my problem is I am very dyslexic, in any documentation I need the format like I just used were an actual example name is used followed by a key that tells you which words in the documentation are not computer code functions.

ie.

var scriptName : ScoreTextScript;
function Update () { scriptName.AddScore(); }
ScoreTextScript.js
var score : int = 0;
function Update()
{
guiText.text = "Score: " + score;
}
function AddScore()
{ score ++;
}

I can see with some effort that scriptName and ScoreTextScript are not code but a fill in the blank type of thing. But in many examples there are so many of these fill in the blanks and they appear deeper into the example that I can’t separate them out in my head.

And lastly this type of thing confuses me very much:

public SerializedProperty longStringProp;
void OnEnable () {
longStringProp = serializedObject.FindProperty (“longString”);
}

it seems that if I were to bring in this “longStringProp” that I should just be able to start using it but everywhere I look it seems to need to be redefined to be able to use

ie. longStringProp =

I can’t get why I need to make it = anything unless I need to change it’s type of container like from a int to a float.

For someone like me I wish there were usage examples where the only colored fonts were the fill in your own blank words.

ie.

var scriptName : ScoreTextScript;
function Update () { scriptName.AddScore(); }

I would be able to read that in 2 seconds like most of you can just with normal examples.

This might have been too much to ask but like most things things are not hard once you understand them so I was hoping someone would have not only a grasp on how Unity C works but understand my issue enough to make a nice bridge of understanding for me :slight_smile:

If i get it right you want refference to script B so you can call B functions in script A?

In script A in initialization you just set public (Class name of B) some variable name (In this case if B script is called B it would look like this - public B bref;

and in editor in inspector you just drag desired script to that field

and for use in script A (for example i want call Shoot() function from B script) bref.Shoot();

but you have to be carefull as you cant drag prefabs to objects that are in hierarchy and vice versa (someone more expirienced should explain that part as i dont know that either)

hope it helps

Does video sit better with you then writing?

http://youtube.com/watch?v=iO5ADWj9g3Y

Okay I can see that I have not gone and made a class at all for my B.cs that has the code I wish to reference.

What is throwing me a bit is:

public B bref;

What is bref?
is that code I am not familiar with? is it short for something?

is it a user defined word that you just happened to choose like from my previous example

ie. public B bref;

I just wish that the IDE would have a unique color for user created things whither it be a namespace, class or whatever.

Unity knows if the names are built in or if I made them in my code…

it’s more then just Unity though… I have a hard time seeing the “user defined” portion in examples and tutorials that I am looking toward to teach me.

@Kiwasi ya that guy is completely correct in that I can see how it is the most common Unity problem LOL

Would have been nice to see actual code that was used to address it though :wink:

I also know that lots of IDE’s can be set up to color things in personal ways… I’m not sure if it is however possible to have it color in the way I suggest but it’s a mute point as I need the reference and tutorials to be doing it already.

The key to telling which part of the code is user defined and which part are engine key words is typically word order. This will be a challenge for you, but you are going to need to require a ton of guts and determination to learn to code. Everything is based on strict adherence to syntax, and a misplaced word or semicolon makes it all meaningless.

That said you are probably onto something with the IDE. Most code editors can be set to colour variables different to types. You can always copy the examples in to get the colouring.

You are going to want to learn the difference between a type and an instance. There should be plenty of google results to help.

The general c# syntax for declaring a variable goes like this.

modifiers Type variableName;

Every user defined variable will have the same sequence somewhere before it is used. Modifiers are optional, and are keywords defined by the language. Types and variable names can be user defined or built in.

Thanks for the help BoredMormon… It never dawned on me to plug the examples into the IDE to get the color help GREAT IDEA thx.

It sounds like perhaps I know less then I do but you are correct that I need to bone up on this in order to get anything done…

The thing is I could pick up on this so incredibly fast if the IDE would have the ability to color user defined code as I am suggesting.

All I need is 3 colors. Color 1 (black is best) code not understood by IDE. Color 2 (what ever really) code understood by IDE and Color 3 (user defined code) I don’t care if it’s a class or a list or a function… if the IDE understands it and it’s not built in the 3rd color would be used.

You might not be getting it but take it from someone dyslexic having things labeled in a way that makes sense to how “you” see things is not only a personal choice but helps “you” get past any difficulties.

I am smart enough to see why I have a hard time but that does not make it any easier lol

This 3 color thing would for me personally untwist my confusion of this particular trouble :smile:

I don’t have any personal experience with challenges like this, but I admire those who do and overcome them anyway. My dad is mostly blind and going deaf as well, yet he is still managing to develop apps and has taught himself morose code to communicate with phones and computers.

It might also be worth googling around to see of there is a specific support group for dyslexic coders. I’m sure there will be someone who has developed specific IDE assistance tools for just this purpose.

Problem is, there is no such thing as “user defined code”. From the IDE’s perspective, there really isn’t a difference between an identifier (which is programmerspeak for a name assigned to a thing in a program) you defined, an identifier some hack at Unity Technology defined and an identifier some hack at Microsoft defined. They are all three just identifiers, and the stuff they point at is pretty much the same as the stuff you write yourself.

In your example,

function Update () { scriptName.AddScore(); }

there is one language keyword, and that is “function”. You can’t replace it with anything else, or your code will fall over and die. Update is an identifier, scriptName is an identifier, AddScore is an identifier. They are just names that refer to stuff, and any name is valid. It’s just that AddScore is the name for something that Unity defined and scriptName is the name for something you defined yourself. And you could call your function “Cat ()” instead of “Update ()”. It wouldn’t work (because Unity couldn’t find your function under that label), but it would still be totally valid C# code, with nothing that could indicate to the IDE or to the compiler that the name you chose is somehow wrong.

I’m afraid you’ll just have to learn to understand these things. There’s no easy way around that.

edit: fixed typo

Well Like I said I know how my thinking is going wrong.

I was avoiding asking for direct help because “do this for me” does not help as much as “help me understand”

But perhaps I can dissect what I need with direct help

How would I get access to these strings from another script?

That might be true however it is very possible for an IDE to pay attention to any “user defined code” in a working folder as apposed to any other folder where it gathers code.

Possible in the sense that if someone wanted to make an IDE that could do this.

Debug.Log(GetComponent<A1>().FieldAlignment[2]); // this line will output "Fire" to the console...
Debug.Log(GetComponent<A1>().Adjcent_Fields[0]); // and this line will output "A1"

The name in the <> brackets is the name of your script’s class. So, if you want to do something with the Transform of a component, you would use GetComponent().DoSomeStuff() instead.

Of course, this only works if the script is attached to a GameObject that also has a A1 script attached to it. It gets a bit more complicated if you are also accessing another GameObject.

1 Like

Ah there is where I went wrong… Debug.Log would never had dawned on me as anything but a tool to throw up an error message.

I will look into this command thx.

// from a script on the same GameObject, inside a relevant method
String myString = GetComponent<A1>().Adjacent_Feilds[0];

// And to test it use
Debug.Log(myString);
1 Like

That is exactly what it does… I used it just as an example. You can put the argument “GetComponent().FieldAlignment[2]” in any context you want. Assign it to a variable, pass it to any function that takes a string as an argument, or whatever. It’s just a building block, and you can connect it to any building blocks with the appropriate LEGO studs.

1 Like

thx BoredMormon, ya that makes it easy for me to really work with the data with a new identifier (Name) or what ever you call it :wink:

ah “to test it” does not mean allowing usage it’s actually just to make sure there is a connection lol

I thought it was needed to “load” it to this other script or something

Thanks to everyone…

I have been watching a C# video this whole time… 6 Hours… way more then I need but I can’t know where I will find the exact thing that will pop all this into my brain correctly :smile:

I dont know if it is good method but i loved to use booleans to communicate with other scripts (as i had no worries if its prefab or not). I made GameManager script and define public static bool in them

public static bool varName;
public static bool varName2;
public static bool varName3;

So when script A need to tell script B to do something, Script A would set bool to true, B would recognize this (in update), call needed function in script B and set bool back to false.

// in A script
GameManager.varName = true;

//in B script update function
if (GameManager.varName == true){
   Function();
   GameManager.varName = false;
}

This is not recommended method but those functions were called occasionally and game was not intensive so I didn’t had to worry much about performance

OMG!!!

I think I just got my answer…

Is it true that in any C# reference manual that a user defined name will start with lower case (Camel case) with at least one uppercase letter?

ie. myScript, tryThis, scriptName…

Lastly will a user defined example never be without an uppercase with a short name

ie. name, text, operator…

It will come. Figuring out the difference between types and members and instances take a bit of getting used to. But once it clicks it will click for good. OOP is a pretty powerful programming system.

Inset evil laugh Then we will introduce you to reflection, were you can treat types as instances. You’ll find out we’ve been lying all along and nothing is what it seems. Everything will get all messed up all over again.