Jickery
Jickery

Reputation: 247

how to add C# script that creates and Instantiates a Game Object in Unity game

I know that scripts are added as components to game objects but I have created a game object using C# script. This is the only game object in my simple test game. How should I "add" it to my game? Please see code below:

public class TestingHeroPositions : MonoBehaviour {

GameObject hero;
Sprite heroSprite; 

void Start () {

    heroSprite = Resources.Load <Sprite> ("Sprites/heroImage");
    SpriteRenderer renderer = hero.AddComponent<SpriteRenderer>();
    renderer.sprite = heroSprite;

    Camera camera = GetComponent<Camera>();
    Vector3 heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, camera.nearClipPlane));

    Instantiate (hero, heroPosition, Quaternion.identity);

    }

}

Upvotes: 0

Views: 2123

Answers (3)

Marko vlaić
Marko vlaić

Reputation: 316

Before instantiating the object add heroInstance.AddComponent<MonoBehaviour>(this);. This should work :)

Upvotes: 1

Aizen
Aizen

Reputation: 1825

You have 3 Major Problems. It is the flow of your code.

1 . You're adding a component to an Object Class not a GameObject Class. The AddComponent is a member class of GameObject.

  1. Like the top problem, Since your hero is a GameObject. You are able to set AddComponent member of it. But the truth is, it is not Getting Instantiated yet.

  2. Instiate is member of Object Class not GameObject class, thus it returns an Object class.

To solve.

public class TestingHeroPositions : MonoBehaviour {

GameObject hero;
Sprite heroSprite; 

void Start () {





    Instantiate (hero, heroPosition, Quaternion.identity) as GameObject;
     //Instantiate first then type cast it to GameObject. Instiate returns Object not gameObject.
     //No need for `new GameObject()` Constructor.

    Camera camera = GetComponent<Camera>();
    Vector3 heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, camera.nearClipPlane));
heroSprite = Resources.Load <Sprite> ("Sprites/heroImage");
        SpriteRenderer renderer = hero.AddComponent<SpriteRenderer>();
        renderer.sprite = heroSprite;

        }

    }

Upvotes: 2

Tobias Wehrum
Tobias Wehrum

Reputation: 1

Simple: You create a new game object in your scene, call it (for example) "Hero Spawner" and attach your "TestingHeroPositions" to the "Hero Spawner".

If you want to create multiple heroes, that is the way to go, although your script should be slightly different:

public class TestingHeroPositions : MonoBehaviour {

    GameObject heroPrefab;
    Sprite heroSprite; 

    void Start () {
        Camera camera = GetComponent<Camera>();
        Vector3 heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, camera.nearClipPlane));

        // Instantiate a new instance of heroPrefab into the scene
        var heroInstance = Instantiate (heroPrefab, heroPosition, Quaternion.identity);

        // Only add the hero sprite renderer to THIS instance of the hero Prefab
        heroSprite = Resources.Load <Sprite> ("Sprites/heroImage");
        SpriteRenderer renderer = heroInstance.AddComponent<SpriteRenderer>();
        renderer.sprite = heroSprite;
    }
}

If you only want to create one hero, you might want to consider letting the Hero object itself (via a component) decide which sprite it has. And if that sprite never changes, maybe just add it to your hero prefab.

Upvotes: 0

Related Questions