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 :frowning:
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

  • 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.

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 ! :slight_smile: