0

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);

    }

}
7
  • Create an empty gameobject and put the script on it. Commented Apr 2, 2016 at 12:20
  • I am getting a NullReferenceException: Object reference not set to an instance of an object error pointing to the line SpriteRenderer renderer = hero.AddComponent<SpriteRenderer>(); Commented Apr 2, 2016 at 12:41
  • @Jickery Your hero gameobject is null... Either make it public and set it in inspector or use GameObject.Find Commented Apr 2, 2016 at 12:47
  • @MartinMazzaDawson is solved it by adding hero = new GameObject();. Though it's working but there's another error which says MissingComponentException: There is no 'Camera' attached to the "GameObject" game object, but a script is trying to access it. You probably need to add a Camera to the game object "GameObject". Or your script needs to check if the component is attached before using it. TestingHeroPositions.Start () pointing to the line Vector3 heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, camera.nearClipPlane)); Commented Apr 2, 2016 at 14:35
  • @Jickery The error is self explanatory. You're trying to access the camera component on this gameobject when their isn't one.. This line GetComponent<Camera>() Commented Apr 2, 2016 at 14:37

3 Answers 3

2

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;

        }

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

1 Comment

Thanks, you have used heroPosition without declaring it, so it's giving a NullReferenceException error
1

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

2 Comments

No it wont. Because the object is not Instantiated and it is not casted to GameObject.
Sorry, than add ´heroInstance.AddComponent<MonoBehaviour>(this);´
0

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.

Comments

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.