Accessing own GameObject

I’m trying to use SpriteManager. The AddSprite() function asks me to pass a GameObject as first parameter, which is the GameObject owner of the script that is calling AddSprite(). How can I reference the own GameObject? Casting doesn’t seem to work.

This is what I have so far:

using UnityEngine;
using System.Collections;

public class Character : MonoBehaviour {

// Use this for initialization
void Start () {
    SpriteManager spriteMan = GameObject.Find("SpriteManager").GetComponent<SpriteManager>();
    GameObject go = (GameObject) this; // doesnt work
    spriteMan.AddSprite((GameObject)this, 10, 20, new Vector2(0.5f, 0.5f), new Vector2(0.5f, 0.5f), true); // doesnt work
    Debug.Log(spriteMan.allocBlockSize);
}

// Update is called once per frame
void Update () {

}

}

1 Answer

1

To return a component’s gameObject, just use the lookup variable inherited from component-

this.gameObject

or just

gameObject

This returns the gameObject that the monoBehaviour is connected to.