Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How to create and instantiate game object from C# script

Discussion in '2D' started by nowletsstart, Apr 5, 2016.

  1. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    I am creating a game object using C# script in the following way:

    Code (CSharp):
    1.  using UnityEngine;
    2.   using System.Collections;
    3.  
    4.   public class TestingPositions : MonoBehaviour {
    5.  
    6.   GameObject hero;
    7.   Sprite heroSprite;
    8.   Vector3 heroPosition;
    9.   // Use this for initialization
    10.   void Start () {
    11.   Instantiate (hero, heroPosition, Quaternion.identity);
    12.   Camera camera = GetComponent<Camera>();
    13.   heroPosition = camera.ScreenToWorldPoint(new Vector3(Screen.width/2, Screen.height/2, camera.nearClipPlane));
    14.   heroSprite = Resources.Load <Sprite> ("Sprites/heroImage");
    15.   SpriteRenderer renderer = hero.AddComponent<SpriteRenderer>();
    16.   renderer.sprite = heroSprite;
    17.   }

    I am getting the following error when I run the app:

    Code (CSharp):
    1.   ArgumentException:The thing you want to instantiate is null.  `UnityEngine.Object.CheckNullArgument(System.Object, System.string message)
    pointing to the line:

    Code (CSharp):
    1.   Instantiate (hero, heroPosition, Quaternion.identity);
    How can I solve this issue?
     
    Last edited: Apr 5, 2016
  2. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    Instantiate is used to make a new instance (clone) of an existing game object or prefab. If you want to make a brand new game object, you can do this to make a game object with a sprite renderer component:

    Code (CSharp):
    1. GameObject hero = new GameObject("Hero", typeof(SpriteRenderer));
     
  3. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78

    Basically, I added my C# script to an empty game object in the inspector. I believe I am suppose to replace the Instantiate with the line of code you stated above, which is what I've done, but now I am getting the following error:

    MissingComponentException: There is no 'Camera' attached to the "Hero" game object, but a script is trying to access it.
    You probably need to add a Camera to the game object "Hero". Or your script needs to check if the component is attached before using it.
    TestingPositions.Start () (at Assets/Scripts/TestingPositions.cs:42)

    I already have the default Main Camera in the inspector
     
  4. nowletsstart

    nowletsstart

    Joined:
    Jan 1, 2016
    Posts:
    78
    I've solved it, I accessed the main camera through it's tag
     
  5. LiterallyJeff

    LiterallyJeff

    Joined:
    Jan 21, 2015
    Posts:
    2,802
    You can use "Camera.main" to reference the current camera in the scene with the MainCamera tag.