I want to instantiate some object. But Unity alwys sais “ArgumentException: The Object you want to instantiate is null.” I HAVE this prefab in Resources folder, and names are correct i check it. My code:
public void Load(string savename){
XElement AllObjects = null;
if(File.Exists(path + "/Savedata/" + savename + ".xml")){
AllObjects = XDocument.Parse(File.ReadAllText(path + "/Savedata/" + savename + ".xml")).Element("AllObjects");
}
foreach (GameObject obj in savable) {
Destroy (obj);
}
foreach (XElement instance in AllObjects.Elements("instance")) {
LoadObject (instance);
}
}
private void LoadObject(XElement obj){
Vector3 position = Vector3.zero;
Quaternion rotation = Quaternion.identity;
position.x = float.Parse(obj.Attribute("x").Value);
position.y = float.Parse(obj.Attribute("y").Value);
position.z = float.Parse(obj.Attribute("z").Value);
rotation.x = float.Parse(obj.Attribute("rotx").Value);
rotation.y = float.Parse(obj.Attribute("roty").Value);
rotation.z = float.Parse(obj.Attribute("rotz").Value);
Debug.Log (obj.Value + "Here!!!!!!!!");
Instantiate (Resources.Load(obj.Value) as GameObject, position, rotation);
}
It`s simple: i called the resources like “Resouces” facepalm
I’m having this error:
ArgumentException: The prefab you want to instantiate is null.
UnityEngine.Object.CheckNullArgument (System.Object arg, System.String message) (at C:/buildslave/unity/build/artifacts/EditorGenerated/UnityEngineObject.cs:104)
UnityEngine.Object.Instantiate (UnityEngine.Object original, Vector3 position, Quaternion rotation) (at C:/buildslave/unity/build/artifacts/EditorGenerated/UnityEngineObject.cs:83)
DefenderSpawner.OnMouseDown () (at Assets/Scripts/DefenderSpawner.cs:22)
UnityEngine.SendMouseEvents:DoSendMouseEvents(Int32, Int32)
and this is the code it’s referring to:
DefenderSpawner.cs:
using UnityEngine;
using System.Collections;
public class DefenderSpawner : MonoBehaviour {
public Camera myCamera;
private GameObject parent;
void Start () {
parent = GameObject.Find ("Defenders");
if (!parent) {
parent = new GameObject("Defenders");
}
}
void OnMouseDown () {
Vector2 rawPos = CalculateWorldPointOfMouseClick();
Vector2 roundedPos = SnapToGrid (rawPos);
GameObject defender = Button.selectedDefender;
Quaternion zeroRot = Quaternion.identity;
GameObject newDef = Instantiate (defender, roundedPos, zeroRot) as GameObject;
newDef.transform.parent = parent.transform;
}
Vector2 SnapToGrid (Vector2 rawWorldPos) {
float newX = Mathf.RoundToInt (rawWorldPos.x);
float newY = Mathf.RoundToInt (rawWorldPos.y);
return new Vector2 (newX, newY);
}
Vector2 CalculateWorldPointOfMouseClick () {
float mouseX = Input.mousePosition.x;
float mouseY = Input.mousePosition.y;
float distanceFromCamera = 10f;
Vector3 weirdTriplet = new Vector3 (mouseX, mouseY, distanceFromCamera);
Vector2 worldPos = myCamera.ScreenToWorldPoint (weirdTriplet);
return worldPos;
}
}