Instantiate object in 2D game?
I have a prefab I'm trying to instantiate when I enter a trigger in a 2D game. I get the following errors:
Assets/Meep/Dialogues/hoardstart.cs(15,37): error CS0103: The name 'hoard' does not exist in the current context.
Assets/Meep/Dialogues/hoardstart.cs(15,44): error CS0119: Expression denotes a type', where a
variable', value' or
method group' was expected.
Assets/Meep/Dialogues/hoardstart.cs(15,25): error CS1502: The best overloaded method match for 'UnityEngine.Object.Instantiate(UnityEngine.Object, UnityEngine.Vector3, UnityEngine.Quaternion)' has some invalid arguments
Assets/Meep/Dialogues/hoardstart.cs(15,25): error CS1503: Argument #1' cannot convert
object' expression to type `UnityEngine.Object'
Here is my script:
using UnityEngine;
using System.Collections;
public class hoardstart : MonoBehaviour {
//private bool bShowGUI = false;
void Awake(){
// You must initialize Dialoguer before using it!
Dialoguer.Initialize();
}
void OnTriggerEnter2D (Collider2D other){
if (other.tag == "Player") {
Dialoguer.StartDialogue (11);
Destroy(this.gameObject);
Instantiate(hoard, Vector2 (x, y, 0), Quaternion.identity);
}
}
}
Answer by ramp · Dec 05, 2014 at 01:14 PM
Hi,
i have resolved all issues,try to use latest updated script.
using UnityEngine;
using System.Collections;
public class hoardstart : MonoBehaviour {
//private bool bShowGUI = false;
public GameObject hoard;
void Awake(){
// You must initialize Dialoguer before using it!
Dialoguer.Initialize();
}
void OnTriggerEnter2D (Collider2D other){
if (other.tag == "Player") {
Dialoguer.StartDialogue (11);
Destroy(this.gameObject);
Instantiate(hoard,new Vector2 (other.transform.position.x, other.transform.position.y), Quaternion.identity);
}
}
}
Thanks
Ram
Answer by HarshadK · Dec 05, 2014 at 08:47 AM
There are two things you need to do:
Declare a variable named hoard and assign it the gameobject/prefab you want to instantiate.
Your Vector2 from Instantiate should be Vector3.