Get the GameObject, the Script is attached to

hi there…

i created a script, that will be attached to EVERY mapobject in my levels. So i need this script to get it´s own “holder” by itself.
What i mean is something like the “this” function.

Example Situation:

Created a cube → attached the script to it
Saved the cube as prefab
Create 32x32 of those cube prefabs → save as new prefab
Build map out of those mass-cube-prefab

now, i just want to edit the single script for ALL cubes. but i need to get the Transform and GameObject of each cube, by calling it´s script like:

using UnityEngine;
using System.Collections;

public class BlockStats : MonoBehaviour {
	
	Color32 basic_color = new Color32(150,150,150,255);
	Color32 highlightcolor = new Color32(250,250,150,255);
	GameObject SelfObject = this.gameObject; //Get the GO this script is attached too

I know that “this” is highlighted blue… But wether the script reference told something about ti… nor this error:

Is there a way to do what i need?

just saying
gameObject
is the GO tha tthe script is attatched to
so gameObject.GetComponent gets other components on the same GO
gameObject.tag is this GOs tag
etc

1 Like
Transform _transform;
GameObject _gameObject;

void Start()
{
   _transform = transform;
   //or if you want it to be the root transform...
   _transform = transform.root;


   _gameObject = _transform.gameObject;

   

}

thank you :slight_smile: