What's wrong with this Instantiate code?
Is something wrong with this code ? It's currently not working and I can't figure out why :( The point is simply when you click on a button, it spawns a tree.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.EventSystems;
public class NewTree : MonoBehaviour {
public GameObject myTree;
public Toggle treeToggle;
public void OnPointerClick(PointerEventData eventData) {
Instantiate (myTree, new Vector3 (0, 0, 0), Quaternion.identity);
}
}
Thanks for your help
Answer by UsmanAbbasi · May 12, 2016 at 06:28 AM
Make sure that the function "OnPointerClick" is being called by attaching a debugger or printing something in console through "Debug.Log()".
Make sure you have passed the prefab into "myTree" variable in inspector.
Does it show any error or simply does nothing? if you are getting any errors then post the errors.
Answer by Moutik · May 12, 2016 at 10:42 AM
Debug was not called, so it made me realize a very simple and yet dumb thing : I forgot IPointerClickHandler in the public class. It now works properly with :
public class NewTree : MonoBehaviour, IPointerClickHandler {
instead of just :
public class NewTree : MonoBehaviour {
Thanks for your answer, it did help me ! :)
Your answer
Follow this Question
Related Questions
Raycasthit explanation wanted. 2 Answers
unity3d instantiate and drag object with mouse 1 Answer
Can't edit new C# files 1 Answer
How to create multiple terrains from one heightmap via C# script? 0 Answers
Does Unity have an equivalent to OSX's Spotlight, Windows 10's Cortana, or Cinema 4D's Commander? 0 Answers