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

Basic Question

Discussion in 'Scripting' started by patmaric, Dec 5, 2019.

  1. patmaric

    patmaric

    Joined:
    Dec 5, 2019
    Posts:
    15
    I am an old-time IBM programmer just getting my feet wet with C+. Someone suggested using Unity as a starting ground, so here I am.

    I am starting with just a basic “click button” program. I don’t get why defined variables stay the same each time I click a button, but arrays come back into the program null.
     
  2. Rocco_B

    Rocco_B

    Joined:
    Oct 28, 2016
    Posts:
    34
    I'm assuming C+ was a typo for C# and not C++.

    Unity could be a good place to get started with C# however it may be worth doing a quick intro on something like Codeacadamy. Perhaps it would also help if you pasted some code of your particular example?
     
    Kurt-Dekker likes this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Welcome! I remember having this EXACT befuddlement, and it took a while, but now it all makes sense.

    Unity is both a coding context and a content authoring system, not just a simple programming interface. The content you are running is precisely as important as the code you are writing.

    Unity has various lifecycles as well has specific rules for managing state within these lifecycles. At certain lifecycle transitions, some state is reset and some state is serialized and deserialized and hence preserved.

    The main thing to be aware of is this overall GameObject lifecycle, as documented here:

    https://docs.unity3d.com/Manual/ExecutionOrder.html

    There is a LOT in there, but don't worry, you needn't concern yourself with much of it initially.

    Another lifecycle to be concerned with is the edit->play->stop lifecycle. Some of it is documented explicitly, some of it you will find scattered around.

    A good overview of that lifecycle can be found by perusing the pages in this hierarchy:

    https://docs.unity3d.com/Manual/UnityOverview.html

    For instance, when you press PLAY in the editor, Unity writes your scene to a temporary scene, then launches it in the editor and lets you do whatever you want, including chaining onto other scenes and running your game almost exactly as it would be run on target hardware in final built form. (or something very close to this!)

    When you press STOP in the editor, Unity destroys everything, then reloads this temporary scene to bring you right back where you were.

    In any case, don't despair, it's a lotta stuff, and I've been using Unity since 2012 and STILL discovering new features, quirks, abilities, etc. Unity3D is an AMAZING beast... hop right in!

    A few other tidbits: infinite loops are verboten, unless they happen in a coroutine. If you ever do a while(true) {} then Unity will lock up on you and you will have to blow it away in the OS process manager. Save your scene often, and use source control (git works great) to creep-and-save your work, in case you fatfinger something in the scene.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,797
    Here's one other key tidbit:

    Unity is a component architecture. Every object in the scene is a GameObject and it has Components plugged into it.

    Components can be pre-built Unity things (such as a Transform or Camera) or your own scripts, i.e., Monobehaviors.

    All other assets are not components but can be dragged into fields in a given component.

    And of course you can always make your own classes that do not derive from Monobehavior, if you don't want to benefit from being part of the Unity scene.
     
  5. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    I don't think I understand the context of this question. Could you describe more about what you're doing; perhaps post your code?

    (Unless one of the replies above already solved things for you, of course.)
     
  6. patmaric

    patmaric

    Joined:
    Dec 5, 2019
    Posts:
    15
    Thank you. I will read through the links you provided. It is definitely a different way of thinking as compared to my background. Little by little, slowly but surely, it’s seeping through.
     
  7. patmaric

    patmaric

    Joined:
    Dec 5, 2019
    Posts:
    15
    Thanks. OMG, this is funny, for some reason I thought it was C+, and that C++ was the more recent version. And every time I tried searching on “C+“, C++ would come up, so I learned to search on “C sharp”.
     
    ProtagonistKun and Kurt-Dekker like this.