New to Unity and coding, need references

Hi all, I’m new to Unity and coding, I am hoping to find references that arent outdated. A lot of things I’m trying to do, I spend a great deal of time trying to follow unitys own documentation then it doesnt work. Then I go down a lengthy rabbit hole of tutorials etc to find the solution to the problem. Then that solution does not work. Then I go down another rabbit hole of why that solution does not work to find that UNITY has changed it, its just done like this now… ffs.

for example, im trying to message a game object that it has been negatively impacted by another game object, it has taken damage or whatever… theres no receiver… theres many posts… but then various posts, “this is outdated” and another said “i just ripped it all out, i dont like send message” or something similar…

So Id love to beat my head against the wall to learn something that doesnt work but perhaps i could be beating my head against the wall to learn something that does work now instead?

Where do I find the direction to what is working now?

Some more background, or venting… I am left handed and usually re-assign all the keys differently straight away. I like having control of which button has what effect so I dont like unitys user input system. id prefer to type it into the code for now. Not that it isnt a great system but i am just learning and i want to see what is doing what.

also on that topic, Im afraid the more i learn about unity, it starts to feel a bit like unreal engine in that somethings being done in the editor and being removed from the code, a certain flavor finds its way to the end product because, “why re-invent the wheel” which is what i was hoping to avoid by leaving unreal to try to learn more over here.

Feeling frustrated and defeated.

I honestly would like to just learn low level coding and do it all myself not to be spiteful but I can’t follow it. I am not understanding. It would take me years to make a pong game but if i understand what im doing it might be more rewarding than clicking a button in the editor that youtube tells me to click.

Thanks for listening, sorry for venting.

Anyway some more up to date guidance? I’ll try to help on my end by paying more attention to search result dates etc…

My suggestion to you is to approach everything incrementally, and to work always using source control so you can trivially recover from errors you make.

Even professional developers don’t make “all the things” in one day. Neither do they stick with the same plan they had on day 1. Every piece of interactive software development is … interactive. You iterate. You try stuff, you see how it works out, if it seems useful, continue using it, if it starts to limit your progress, you back up and refactor.

All through this process you will be able to gain valuable knowledge about how to do stuff. Following tutorials may also be useful, just to see how others do it, but when you do, be sure you are doing tutorials correctly, or you might as well not even bother with them (see below).

Finally, when you say this:

That is so vague as to be completely not actionable.

  • do you mean a chess pawn capturing / taking another chess piece?

  • do you mean a stone bumping into a ball of chalk and exploding it?

  • do you mean a spaceship shooting an asteroid?

  • do you mean a fully-equipped armored knight swinging a flail tearing limbs off a giant spider one at a time?

You can imagine each of these things has completely different answers.

So… get busy with some existing good tutorials, but make sure you’re not wasting your time:

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Tutorials are a GREAT idea. Tutorials should be used this way:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

1 Like

Ha! sendMessage is a very old “game programmer” thing which Unity copied from other game engines. The idea is that game programmers know just enough to get by and can’t do it the good way. If the score increases they use just sendMessage("scoreIncrease") (or whatever – I’ve never used sendMessage). Over in the scoreBoard it has something named scoreIncrease which receives the message. To some people, that seems simpler – it skips the step of having to know who should get the message. You just send it out and the system figures out who wants it.

It’s out-dated because enough people used Unity and as a group realized it was easier to learn regular programming. To talk to someone else you need a variable variable pointed at it – like monsterA. the variable is usually set in code, but Unity allows you to auto-set it from the Inspector:

// drag in monster gameObject with monsterScript on it,
// creates a link to the script for that monster:
public monsterScript monsterA; // variable is monsterA, type is monsterScript

  // in some function:
  // use the link to directly call takeDamage() for that monster:
  monsterA.takeDamage(4);

Again, any programmer will understand all of this except they’ll have to ask “does Unity have some special way of setting monsterA to a monster?”

As far as tutorials, especially videos, they all never seemed very good to me. Most of Unity is just learning to program. Books are better at that (but there are lots of bad ones). The rest is in the current Unity API. It doesn’t give examples, but if you can program then it tells you enough.

1 Like

Thank you very much for the feed back.
There are many rays of hope here. I have in the meantime found that unity has quite the library of tutorials. I should have spent more time there when I downloaded unity. I downloaded unity and unreal and blender and a few others that i deleted shortly after years ago. I have spent 90% of my time since playing with blender, fleshing out ideas. then maybe 9% trying to make it work in unreal. 1% for random joke if i had a better sense of humor.

About 3 or 4 months ago, I made a choice to abandon blenders engine and spent a lot of time deciding on an engine and chose unity. I knew thered be a lot to learn… so im trying to force myself to illustrate the ideas and solve the problems with unity. So far ive only used blender once since to illustrate something (game engine). Blenders great and will always use it to make stuff though (objects etc).

I meant it as a generic example, i dont need to solve the message issue. In a more generic sense, I dont know how to communicate within this frame work. I am used to blender. In blenders logic block system, you can message an object specifically or simply yell something out. So if you message “increase” the score goes up, the health goes up, the level goes up… everything that is actionable upon the word “increase” but if you message the scorekeeper “increase”, only the score goes up the level and health stay the same. Im just not understanding the unity C# world enough yet to make that happen. But frustratingly, I have an idea that it can be done.

I also have a messed up expectation of the time frame for these tasks. A couple of months ago I thought Id make a simple cube with a camera so i could see what im doing within a project while im working and thats pretty much where i still am. I have learned a lot but it was more than i thought itd be. I have (maybe to my detriment) gotten so used to blender that i could do the same thing there in minutes.

I have made many versions of a block and camera, none perfect but good enough to see it from a player perspective for now.

The method of communicating that you describe, Owen Reynolds, I will play with. Thats maybe what I was looking for but I cant imagine what I would of had to search to find that. Message without the message command? lol… maybe… ALSO, (edit) if you make scripts public like that do they become taxing on the system? This is honestly years away from where I am but just curious, I imagine I’ll get to streamlining for efficiency someday but no time soon.

Thanks again, sorry for my frustration. This should get me hours and hours onward so I’ll try not to be a burden for a while.

From what I remember, Blender is more of a traditional game engine. One where sendMessage is a normal thing, made up just for game programmers. But to normal programmers sendMessage is very confusing – somehow someone else magically gets the message? To a programmer the thing where you “message” only the scorekeeper makes more sense. There’s no magic step about who you’re talking to. And Unity is more for normal programmers. Some of the things you learned in Blender’s engine are going to be more difficult, but more flexible once you learn them.

There are a few main ideas and methods for sending messages from one place to another in Unity.

  1. Public variable, inspector injection
    This one is the most common when just starting out. *Most of the time, when you make a public variable in a script, that variable will be exposed in the editor. You can then click and drag an object from the scene or project into that variable and Unity will inject the reference. A slightly more advanced version of this is to use a SerializedFieldAttribute on any variable, not just public ones.
public class MyScript : MonoBehaviour
{
    public Trasform referenceToAnotherObjectsTransform; // Leave this uninitialized, assign it in the editor
    [SerializedField] private Vector3 somePositionInSpace; // Also assigned in the editor, I prefer this most of the time

    private void Update()
    {
        referenceToAnotherObjectsTransform.position = somePositionInSpace; // Using both "uninitialized" variables, works fine unless you forget to assign the transform in the editor
    }
}
  1. GetComponent()
    This one is used in a lot of ways; if you already have a reference to a GameObject, calling this can get you a specific type of component attached to that GameObject. One of the more common uses is to call this in the Awake method to grab a component attached to the same object as the current script.
public class Weapon : MonoBehaviour
{
    public string weaponName; // Assigned in the editor
}
public class Player : MonoBehaviour
{
    private Weapon playerWeapon; // Not assigned in the editor because it isn't public and isn't a [SerializedField]

    private void Awake()
    {
        playerWeapon = GetComponent<Weapon>(); // We MUST assign a reference before using playerWeapon, so we look for one that is attached to the same GameObject as the Player script
    }

    private void Update()
    {
        Debug.Log("Player has the weapon: " + playerWeapon.weaponName);
    }
}
  1. Physics events, such as OnTriggerEnter
    This topic is a bit too big to cover well in a post, I recommend digging deeper elsewhere when you want to learn more. However, the basic idea is that objects with colliders (and rigidbodies**) can send different events to each other when their colliders interact through the physics system.
public class DebugOnTrigger : MonoBehaviour
{
    private void OnTriggerEnter(Collider other)
    {
        Debug.Log(other.gameObject.name); // Print the name of the game object that entered our trigger volume. A trigger volume is just a collider with "isTrigger" set to true
    }
}
  1. UnityEvents
    This is a more advanced topic, but a pretty useful one if you are comfortable programming in other environments and like decoupling your code. UnityEvents allow you to trigger code from other scripts without either script knowing about the other. The link between them is established through the editor, or through code.
using UnityEngine.Events;
public class FireEventOnUpdate : MonoBehaviour
{
    public UnityEvent onUpdate; //
    public UnityEvent<string> onStringUpdate; // methods that take a string parameter
    [SerializedField] private string mySecretString; // assigned through the editor

    private void Update()
    {
        onUpdate.Invoke(); // onUpdate will be populated with functions/methods to call through the editor. We don't need to know anything about those methods; there could be zero or a million of them that all do different things.
        onStringUpdate.Invoke(mySecretString);
    }
}
public class PrintString : MonoBehaviour
{
    [SerializedField] private string toPrint;

    public void Print()
    {
        Debug.Log(toPrint); // A silly example, but the method signature is very common and could contain almost any code you could want. This method could be assigned through the editor to the UnityEvent
    }
}
public class PrintSecretString : MonoBehaviour
{
    [SerializedField] private FireEventOnUpdate updateScript; // assigned through the editor. This breaks the "don't know about each other" part of Events, but I'm including it to show how to set up a listener method through code and not the editor.

   private void Awake()
   {
        updateScript.onStringUpdate.AddListener(ExposeSecret); // we add a method that takes exactly one string as a parameter as a listener. That method will then be called whenever updateScript fires it's string event.
   }

    private void ExposeSecret(string nameDoesNotMatter)
    {
        Debug.Log(nameDoesNotMatter);
    }
}

These aren’t all the possible ways, but you should be able to do anything with just this. Good hunting, keep at it and you’ll get through the initial learning curve in no time.

  • Some things cannot be serialized; there are some complicated rules around it, but basically anything derived from a Unity class should work, and primitives like int and string should work. Plain C# classes will probably work, but watch out for classes that reference themselves, like a linked list. That can cause trouble.
    ** Whether or not different physics events get triggered depends on what kind of colliders are involved, whether or not there are rigidbodies attached to each object, and how each rigidbody is configured. It’s a bit topic that I always use a reference sheet for. Unity - Manual: Introduction to collision

You always copy and paste this message. Please give helpful information and don’t spam your comments everywhere. Because i’m sure this didn’t help him. You just gave global information not an answer

Are you serious?? The first ELEVEN (11) lines I wrote above were custom written for OP and tailored EXACTLY to his question.

After that I pasted the standard “how to do tutorials properly” because… guess what? A massive proportion of people posting here report that they just don’t get anything from “watching tutorials,” and we know that’s not the right way to do it.

Sounds like you are not reading things properly? Perhaps you should consider reading Step #2 in my “how to do tutorials” above. :slight_smile:

1 Like

Unity gets lots of Q’s which are basically “I’m having trouble, how do I learn Unity?” In theory it’s better to paste one pre-written carefully edited reply. It’s like if someone asks how for-loops work – better to put a link to a good spot than write it from scratch.

But, here’s how to properly use Unity tutorials: from the end, without pressing play, click backwards until you see a screen with code (the screens with code before this are partly finished versions of the same thing). Read it. Advanced tutorial use: check whether the final version of the code is written anywhere below the video.

1 Like

The Irony of this response as you are the one who provided absolutely Zero Information on the topic.

Emkay, read Kurts response carefully. He provides excellent guidance into Game Dev (And Coding in general to be honest)

Copy paste messages that are so useful they sometimes get officially pinned.

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Also, I’m not sure that someone with 13 posts in 2+ years is really qualified to be gatekeeping what it means to provide helpful information on the forums.