3

How can I add a variable to a GameObject? GameObjects have a series of variables (name, transform, ...) that can be accessed and modified from any script. How could I add a variable such as for example "color" or "type" that could be accessed or modified from other scripts. Thanks.

3
  • just make your variables public and add your gameObject's reference to them. Commented Nov 24, 2015 at 22:44
  • could you give me an example? I am not sure I understand (I am novice to c#) Thanks Commented Nov 24, 2015 at 23:17
  • 2
    adding an example... Commented Nov 24, 2015 at 23:18

4 Answers 4

3

As every object in c# GameObject class inheritance Object class. If you right click on GameObject and select go to definition you will see that below. enter image description here

In order to reach one object's variables you need a reference(not considering static variables). So if you want to change variables of one GameObject from other you need a ref. enter image description here On Unity inspector you can assign values to public variables of a script. Below you can see Obj end of the arrow. Now assign cube(is a GameObject ) to Obj. enter image description here After drag and drop

enter image description here

Now you can reach every subComponent of cube object. In red rectangle I reached it's Transform component and changed it's position. enter image description here

I hope I understood your question correct.GL.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you Burak for your great answer! I have gone so far and understand this principle you explain. ( i hope it will help other people reading this thread :) ) my question is how could I add a new variable to the GameObject. A GameObject has a set of components such as Transform, Name and others. Now I was wondering if it is possible to add a component such as Color or a date or anything else. But it seems from the others people that answered that it is challenging. Seth bellow posted a link, but for me it is too complex right now.
Thank you. I have searched in Internet about your question after reading your comment. Interesting question. First i looked through internet about adding a special component i couldn't find much so that probably even if exists it is not a common subject. In unity3d I inherited a class from component and added to one of my gameObject programmatically. But it was perceived as Script. Will be searching more, I think it is about base of Unity structure.
Burak, thanks for searching, i have searched for a while too, didnt find much so i posted this question. I asked a derived questions with some code that could be an alternative way to get what i want, pls have a look: stackoverflow.com/questions/33906741/…
2

If GameObject wasn't a sealed class then inheritance could have been the solution to your problem. In this ideal situation you could try the following.

public class ExtraGameObject : GameObject
{
    // Logically you could make use of an enum for the 
    // color but I picked the string to write this example faster.
    public string Color { get; set; }
}

A solution to your problem could be to declare your class like below:

public class ExtraGameObject
{
    public string Color { get; set; }

    public GameObject GameObject { get; set; }
}

I am aware of this that this is not exactly that you want, but that you want I don't think that it can be done due to the sealed nature of GameObject.

3 Comments

thanks, trying to declare a class that inherits from GameObject gives me an error: " error CS0509: ExtraGameObject': cannot derive from sealed type UnityEngine.GameObject' ". I guess like Seth says a variable can't be added to a GameObject.
@cubecube I wasn't aware of this :( I think that my updated post may provides you a solution.
Thanks Christos, I am actually trying to figure out another way to achieve what I need (I am new to c#). I am not sure if it is clear: If I instantiate a GameObject and attach a script to it (with AddComponent), how can I read the variables of that attached script from another script?
1

You can add a script to the prefab from which an object is created using the Instantiate method. In this script, you declare public variable. Then you can change and get the value of this variable using the GetComponent method.

The code in script file added to the prefab (prefab name is thatPrefab, script file name is IdDeclarer) as a component:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class IdDeclarer : MonoBehaviour
{
    public int id;
}

Declaring an object's characteristics (ID in example):

GameObject newObject = Instantiate(thatPrefab, new Vector3(0, 0, 0), Quaternion.identity) as GameObject;

newObject.GetComponent<IdDeclarer>().id = 99;
Debug.Log(newObject.GetComponent<IdDeclarer>().id);

Output: 99

I hope I helped you

Comments

0

Christos answer is correct. You cannot add a variable to the GameObject datatype because it was built into the language as a base. To add variables to premade data type, extend off of them. For more information on inheritance see

https://msdn.microsoft.com/library/ms173149(v=vs.100).aspx

1 Comment

thanks Seth, I am too novice to get into adding variables to premade data type i guess. I need some time to understand but i ll try :) thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.