- Home /
(VERY BASIC) how to assign a variable to a gameobject
so basically i need to make a variable that is assigned to a game object. In this case it is the enemy in a basic pong game i'm working on. the game object is named "enemy" btw. I am VERY new to unity and scripting so please go easy on me. Thanks in advance.
p.s. im using javascript
Answer by Sericet1 · Feb 24, 2013 at 05:45 AM
Create a JavaScript file and place it on the game object just like any other component. You can create variables for that game object by making variables in that script. Is this what your asking?
Answer by Tarlius · Feb 24, 2013 at 07:07 AM
You can't assign arbitrary values directly to a game object, you will need to create a behaviour for the object.
Create a new script file, EnemyBehaviour (for example) and drop it on the game object, as Sericet1 suggested.
Put this in that script:
public int someValue = 0;
Then in the main script, you can access this value by:
public EnemyBehaviour enemy; // swap for whatever you called the class, and drag and drop the enemy into the space in the editor
void Start() {
Debug.Log(enemy.someValue);
}
While I appreciate you said JavaScript, the general idea is the same. I'm not up on Java syntax.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093304im_/https://answers.unity.com/themes/thub/images/avi.jpg)