Accessing both an object and its script using the same variable

Hi,

Seems like a silly question but here goes… I’m using a pool of objects instantiated via a game manager script at Start() time. Some of the parameters of the objects need to be set up when they are enabled. However, the code looks really off because some of those variables are attached to the GameObject (such as “name”) while others are attached to the script component of that object (e.g. “monsterType” or “gender”, it doesn’t matter). So I need two variables, one pointing to the object, the other one pointing to the script. This isn’t very readable (object.name and objectScript.monsterType is really awful to look at). Is there a way to do this more elegantly?

Thanks.

EDIT: Here is a concrete example illustrating the question, which I’m adding to the original post after reading the answers and especially the comment below by @ShadyProductions (indeed, whether I like it or not is not the point, I’m only trying to understand if this code pattern is considered usual or not).

The following in a script attached to an empty GameObject. Its purpose is to create an object pool, instantiate and keep track of several instances of a prefab object.

using System;
using UnityEngine;

public class Manager : MonoBehaviour
{
	public GameObject MonsterPrefab;
	private List<GameObject> monstersPool;

	void Awake()
	{
		// All Monster objects are grouped under an empty GameObject
		GameObject monsterRoot = new GameObject ();
		monsterRoot.name = "Monsters";

		// Create the pool
		monstersPool = new List<GameObject> ();
		for (int i = 0; i < 20; i++)
		{
			GameObject obj = Instantiate (MonsterPrefab, Vector3.zero, Quaternion.identity);
			MonsterController mc = obj.GetComponent<MonsterController> ();
			mc.type = "Whatever"
			obj.name = "Monster " + i;
			obj.transform.parent = monsterRoot.transform;
			mc.DoSomeMethod ();
			obj.SetActive (false);
			monstersPool.Add (obj);
		}
	}
}

The only thing that bothers me here is that I sometimes refer to the object as “obj” and sometimes as “mc” when it is conceptually the same thing (the monster). If that’s the way it should be done, I haven’t the slightest problem with that. It just seems weird and I’m looking for confirmation. Please bear with me, it’s not the simplest thing to learn all this by yourself :slight_smile:

Thanks.

I don’t really understand what you want to happen. so If you can clarify it a bit that would be nice.

What I think you want have is 2 different variable. 1 is reffering to an object and the other is reffering to a component in the object

I have two variables, like you wrote. One pointing to the GameObject, another pointing to the script attached to that object. My problem is not with functionality (everything works as expected). It’s purely a style issue and I am wondering if I’m doing something wrong.

Say you have an Object which represents a thing such as a monster. You want to refer to e.g. its name and position, and you use thing.name and thing.transform. But you also want to refer to its age or eye color or whatever, and you have to use “thingScript.age” because “age” is not a property of the GameObject, but of the script.

So it’s looks, from a “prose” point of view, that you’re referring to the same object (at least, it’s the same in your head, it’s the monster) using two different variables, one for the script and another one for the object.

I understand what this means programatically (I think). I’m just curious if this is common practice or if there’s a way to make it more elegant; unify, if you will, the way you refer to an object by using only one reference variable and all its properties under the same “roof”.

I hope this makes more sense. Pardon me if this seems weird, I’m teaching myself C# and I bump into all sorts of conceptual questions which might seem strange to trained eyes…

Thanks.

if you dont want the obj and mc but still want access, get the mc then use

> mc.gameObject.name = "blah"

you can still access the gameObject and transform of it through the component it’s attached to, however i believe it is better to make the object and script calls the way you are now instead of going through the script component.