I have been stuck on this for 2 days now.
I have written a script that instantiates an object with face-angle-based placement.
I am trying to ensure that only 1 object exists at any one time, but it doesn’t work (not sure why).
All I can tell is that the GameObject array (jps) is not getting populated (something to do with line 27, but no syntax errors).
using UnityEngine;
using System.Collections;
public class RaycastInstance : MonoBehaviour {
public GameObject jumppadPrefab;
private RaycastHit hit;
private Ray ray;
public GameObject[] jps;
public int jpCount;
void Update()
{
if (Input.GetButtonDown("Fire1"))
{
Vector2 screenCenterPoint = new Vector2(Screen.width/2, Screen.height/2);
ray = Camera.main.ScreenPointToRay(screenCenterPoint);
if(Physics.Raycast(ray, out hit, Camera.main.farClipPlane))
{
Vector3 jumppadPosition = hit.point;
Quaternion jumppadRotation = Quaternion.FromToRotation(-Vector3.down, hit.normal);
GameObject pad = (GameObject)GameObject.Instantiate(jumppadPrefab, jumppadPosition, jumppadRotation);
pad.gameObject.name = ("Jumppad");
JumpPad jp = pad.GetComponent<JumpPad>();
jp.isInstantiated = true;
jpCount++;
if (jpCount > 1)
{
jps = GameObject.FindObjectsOfType(typeof(JumpPad)) as GameObject[];
foreach (GameObject jupa in jps)
{
if (jupa.GetComponent<JumpPad>().isInstantiated)
{
Destroy(jupa);
}
}
}
}
}
}
}