- Home /
 
Instantiate a premade GameObject
 using UnityEngine;
 using System.Collections;
 
 public class StartTheBall : MonoBehaviour {
     
     float cacheX;
     float cacheY;
     float cacheZ;
     Quaternion cacheRotation;
     
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
         cacheX = transform.position.x;
         cacheY = transform.position.y;
         cacheZ = transform.position.z;
         cacheRotation = transform.rotation;
         
         if (Input.GetKey(KeyCode.Space))
         {
             Destroy(this.gameObject);
         }
     }
     
     void OnDestroy() {
         Debug.Log("Destroyed!");
         
         GameObject ball = (GameObject)Instantiate(Resources.Load("PlayerBall"), new Vector3(cacheX, cacheY, cacheZ), cacheRotation);
     }
 }
 
               Compiles, the game runs, but then the object is destroyed, instead of creating the premade object, it gives me that:
ArgumentException: The prefab you want to instantiate is null. UnityEngine.Object.CheckNullArgument(System.Object arg, System.String message) (at C:/BuildAgent/work/cac08d8a5e25d4cb/Runtime/ExportGenerated/Editor/UnityEngineObject.cs:103)
The problem, 100% sure is at this line:
GameObject ball = (GameObject)Instantiate(Resources.Load("PlayerBall"), new Vector3(cacheX, cacheY, cacheZ), cacheRotation);
I would appreciate any help!
Well, its right there. You already know. You cant instantiate an object when the GameObject/script is no longer there to call it.
Answer by whydoidoit · Sep 26, 2013 at 04:34 PM
It's no problem instantiating an object there.
So it isn't finding the PlayerBall object in your resources. It is in the root of a folder called Resources? You could also try doing Resources.Load("PlayerBall", typeof(GameObject))
Awesome, problem was folder name! Sorry for that guys, im still learning...
Thanks very mutch for the help!
Your answer