First code line in first component
Let's code!
See below how to add first component. Let say that components are bricks of your game. You can build whole behaviour of object in the scene from components. To learn more about Unity components you can refer to official Unity Manual.
Here is code added to the component:
You don't understand what's going on or you've never programmed? Dont't worry!
Today's programming and programming programming are quite different from those 10 years ago or shown in movies. It is very easy now! And basically you learn by copying the code, modifying it, and observing how it works after the changes.
What's happening in line of code added by us - in the simplest words:
void Start() - this is a part of code, which is run at the same beggining of the initialization of GameObject (it's not really true, but let it be enough for now).
Inside body of Start method we put GetComponent method with indication of Type inside brackets <>. Then is ".color". Then "= Color.red". Let's just read this sentence:
Take part of GameObject to which I am attached of specific type<SpriteRenderer>, then take it's property, color, and set it to red color.
That's it.
It's simple, right?
Try to change Color.red to i.e. Color.blue or sth. And duplicate bricks in the scene with and without attached ChangeColorComponent to observe what's happening.
See below how to add first component. Let say that components are bricks of your game. You can build whole behaviour of object in the scene from components. To learn more about Unity components you can refer to official Unity Manual.
Here is code added to the component:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | using System.Collections; using System.Collections.Generic; using UnityEngine; public class ChangeColorComponent : MonoBehaviour { // Use this for initialization void Start () { GetComponent<SpriteRenderer> ().color = Color.red; } // Update is called once per frame void Update () { } } |
You don't understand what's going on or you've never programmed? Dont't worry!
Today's programming and programming programming are quite different from those 10 years ago or shown in movies. It is very easy now! And basically you learn by copying the code, modifying it, and observing how it works after the changes.
What's happening in line of code added by us - in the simplest words:
void Start() - this is a part of code, which is run at the same beggining of the initialization of GameObject (it's not really true, but let it be enough for now).
Inside body of Start method we put GetComponent method with indication of Type inside brackets <>. Then is ".color". Then "= Color.red". Let's just read this sentence:
Take part of GameObject to which I am attached of specific type<SpriteRenderer>, then take it's property, color, and set it to red color.
That's it.
It's simple, right?
Try to change Color.red to i.e. Color.blue or sth. And duplicate bricks in the scene with and without attached ChangeColorComponent to observe what's happening.
Comments
Post a Comment