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);
}
}
NullReferenceException: Object reference not set to an instance of an objecterror pointing to the lineSpriteRenderer renderer = hero.AddComponent<SpriteRenderer>();GameObject.Findhero = new GameObject();. Though it's working but there's another error which saysMissingComponentException: 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 lineVector3 heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, camera.nearClipPlane));GetComponent<Camera>()