Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Help a n00b

Discussion in 'Scripting' started by pluMmet, Jul 19, 2015.

  1. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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 :)
     
    Last edited: Jul 19, 2015
  2. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    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
     
  3. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  4. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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.

    @BoredMormon 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 ;)
     
  5. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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.
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  7. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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 :D
     
  8. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
  9. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    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,

    Code (CSharp):
    1. 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
     
  10. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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?

     
  11. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133

    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.
     
  12. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    Code (csharp):
    1. Debug.Log(GetComponent<A1>().FieldAlignment[2]); // this line will output "Fire" to the console...
    2. 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<Transform>().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.
     
    Kiwasi likes this.
  13. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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.
     
  14. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Code (CSharp):
    1. // from a script on the same GameObject, inside a relevant method
    2. String myString = GetComponent<A1>().Adjacent_Feilds[0];
    3.  
    4. // And to test it use
    5. Debug.Log(myString);
     
    Taschenschieber likes this.
  15. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    That is exactly what it does... I used it just as an example. You can put the argument "GetComponent<A1>().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.
     
    Kiwasi likes this.
  16. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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 ;)
     
  17. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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 :D
     
  18. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    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
    Code (CSharp):
    1. public static bool varName;
    2. public static bool varName2;
    3. 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.
    Code (CSharp):
    1. // in A script
    2. GameManager.varName = true;
    3.  
    4. //in B script update function
    5. if (GameManager.varName == true){
    6.    Function();
    7.    GameManager.varName = false;
    8. }
    9.  
    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
     
  19. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    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...
     
  20. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.

    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.
     
  21. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    That actually works great for my flow chart of how I wanted to program this so thanks!
     
  22. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    I dont know if you meant that but naming variable nameone or nameOne doesn't have any effect, it is estetic and user preference. (atleast i never found error that said i have to use uppercase)
     
  23. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    I had to watch 1 hour and 30 min of this:



    to see it suggested that user defined camel case starts with lower case lol

    You probable all new that without thinking...
     
  24. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    but can I count on that in official documentations?
    Is it considered such good form that it is standard?

    not needed in IDE....
     
  25. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Most of the time this is true. However it's convention, not a rule. Everybody works this way because we agree that it looks nice. An it makes it easier to read and share. You should work this way.

    But occasionally you will run across code that doesn't follow this convention.

    To formalise the Unity conventions
    • Classes and methods use TitleCase. Like MonoBehaviour or Update
    • Fields and properties use camelCase. Like transform or myRandomInt
     
  26. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    Regarding post #21: Yeah, flags can be useful, but that's not really how modern programming works. Not for performance reasons, but because this approach makes everything more error-prone and harder to debug.

    If ScriptA wants ScriptB to do something, ScriptA should call a method on ScriptB, because that's what methods are for.

    So instead of setting varName to true, you should just do GetComponent<ScriptB>().Function(); Less boilerplate code, you're guaranteed to have Function() called in the same frame (and not in the next frame, as could happen with your approach), and you can have multiple calls to Function() in one frame without having to implement multiple flags for it.
     
    Kiwasi likes this.
  27. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    Dang I thought I was onto to being able to read this stuff without getting confused:

    Time.deltaTime


    Back to the drawing board lol
     
  28. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Yeah i knew this is bad (it's logical it is one big flaw) but I was beggining with C# and I "discovered" it :)
    but how can i refference object in hierarchy to prefab object as it wont accept it?
     
  29. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    Good to know
     
  30. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    Dont worry about Time.deltaTime just copy it and it will come to you in examples. I was bothering with it for quite while and still dont get whole concept (just in rough part)
     
  31. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Time is a class, hence the capital letter. deltaTime is a field or property, hence the camel case. The . in the middle means that deltaTime "belongs" to Time.

    And I this case they are both are members of UnityEngine.
     
  32. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    Time is a class, and deltaTime is a static member of this class (meaning it's a variable that's attached to the class itself, not to each instance of that class like most variables). This is why you can just access Time.deltaTime like that without having to find a certain instance of the class Time first.
     
  33. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    ya i got all that '.' belongs to .. that is great!

    I just learned that 'var' is C# attempt to be like python lol

    I LOVE IT - LMAO.
     
  34. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Yeah, don't use var until you can work fine without it. It's basically there for lazy coders, and it works fine. But you want to learn static typing properly first before you get lazy.
     
  35. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    Well BoardMormon this is where we differ a bit ;)

    I hope in the next release of c# they lose the ' ; ' and the '{ , }' stuff and just go with proper indentation to create capsules.

    I don't find the compilers ability to see that a value is a string or int or float as a laziness at all and it is much appreciated.
     
  36. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    You could use Boo. It's not officially supported anymore, but it still works.
     
  37. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    I was beginning to study it when I found out it was no longer supported... I wish so much that it was.

    Had I already been using Unity I would have no problem using a Boo external IDE but I have to much to learn to have that in my way :(
     
  38. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    unless someone has... didn't I see that you can get unity to use another IDE?

    You can get it to use Visual Studio right?

    if so where is the Boo reference material? Does it exist? when I saw it gone from the Unity web reference that is when I got sad.
     
  39. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    google "boo in unity 5" returns nothing helpful in how to set it up or if the reference is still available.
     
  40. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    I disliked Visual Studio because support for unity was bad for express (you had to get some packages and do some adjustments)

    But new Visual Studio Community is awesome, just set exe of VS in unity and you are good to go, not to mention i can have opet more scripts at same time and edit compared to monodevelop

    Also dont bother with anything beside C# as C# is global language comapred to unity java and boo.
    if you ever need to move to other engine it will be MUCH easier
     
  41. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    Ya I just saw that I can set up other IDE's in Edit>Pref.>External Tools...

    Now I just need the Boo reference material... Anyone?

    Would have been terrible wasteful for the Unity fellows to have trashed it.
     
  42. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    Guess I can't complain maybe they were able to make Unity free but cutting the extreme cost of supporting Boo lol

    back to this f'ing video :p
     
  43. phoda

    phoda

    Joined:
    Nov 11, 2014
    Posts:
    384
    imo creating your games(or what you want to create) and then looking for solution is WAY better than looking at few hours long video or just looking tutorials for some time.
    I watched only Roll a ball and scavanger tutorials of unity and if iam stuck i find tutorial that I need, you will learn more and faster
     
  44. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    They cut support for Boo because Boo is stupid and nobody likes it anyway.

    If you use anything other than C#, you lose some of the most powerful dev tools, like Visual Studio. So most people stick to C# or UnityScript.
     
  45. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133

    Well I need to familiarize myself with this C#... the reason I am having so much trouble getting proper short and to the point answers from reference of a quick tutorial is that when I see something I am unfamiliar with like that 'dateTime' thing I don't know if it is an inherent part of C# or if it is user defined in a place I have not noticed or perhaps even not 'on screen' in the case of a video tutorial... I have to scan the code to see if they created it and my head just spins if I have been at it all day or is just frustrating if I just began.

    I guess I meant 'deltaTime' ;)
     
    Last edited: Jul 19, 2015
  46. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133

    I can't argue with Boo's lack of power I have no idea but the usage of the fore mentioned lack of ' ; ' and '{ , }" and I guess it is like python with it's no nonsense variable detection as well.

    These things are genius to me :D
     
  47. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    C# can do type inferrence, and there are people who find that brackets and semicolons actually make code more readable. It is a lot easier to structure complex instructions if you can use indentions as you please because it is not relevant to the syntax.
     
  48. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133

    Using proper indentation is much preferable to trying to see where I left out a ' } '

    There are people who find the clean look of python to be a respite in a world of noise (like me :D )
     
  49. Taschenschieber

    Taschenschieber

    Joined:
    Jun 8, 2014
    Posts:
    238
    Yeah, maybe for you, but other people would disagree.
     
  50. pluMmet

    pluMmet

    Joined:
    Jul 18, 2015
    Posts:
    133
    I can't argue that :)

    but I still see the beauty:

     
    Last edited: Jul 19, 2015