How to spawn a prefab when a button with a certain tag is pressed
I'm trying to figure out how to spawn a prefab where the camera is looking and when a ui button with a certain tag is pressed. Here is my code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using System;
public class Spawner : MonoBehaviour {
private Vector3 _Position;
public GameObject Prefab;
void OnEnable(Button other)
{
if (other.CompareTag("Cube"))
{
GetPosition();
Instantiate(Prefab, _Position, Quaternion.identity);
}
}
private void GetPosition()
{
Vector3 mousePosFar = new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
Camera.main.farClipPlane);
Vector3 mousePosNear = new Vector3(
Input.mousePosition.x,
Input.mousePosition.y,
Camera.main.nearClipPlane);
Vector3 mousePosF = Camera.main.ScreenToWorldPoint(mousePosFar);
Vector3 mousePosN = Camera.main.ScreenToWorldPoint(mousePosNear);
RaycastHit hit;
if (Physics.Raycast(mousePosN, mousePosF - mousePosN, out hit, 1000))
_Position = new Vector3(hit.point.x, hit.point.y, hit.point.z);
}
}
Answer by Cuttlas-U · Oct 23, 2017 at 07:13 PM
hi; i think for a simple way u can throw away that big GetPosition() function;
just give the camera a collider like sphere collider ;
then make a bool variable to be true each time the collider has enter trigger with your object or cube and false it when it exit;
and then again when u enable just check if that bool is true so u know the camera is looking;
Sorry I can't get rid of that GetPosition() function is what causes the prefab to spawn where the camera is looking, I can't get rid of it. I was looking more at the void OnEnable(Button other). I want it so when I click a UI button, it spawns a prefab where the camera is looking (that's why there is the GetPostion() ). This is a UI function, so there are no colliders.
hi; sorry for late response; ok u wrote your codes like this :
GetPosition();
Instantiate(Prefab, _Position, Quaternion.identity);
after calling GetPosition it will immediatley go to the next lane so u may not get the right position to spawn the object; so what u can do is wait for that function to complete the calculate and sent back the position that we need to Instantiate the obejct:
first change make the function as a return value and at the end return the value it got :
private Vector3 GetPosition()
{
return _Position;
}
then in your Enable code u can do like this:
void OnEnable(Button other)
{
if (other.CompareTag("Cube"))
{
Vector3 PosToSpawn = GetPosition();
Instantiate(Prefab, PosToSpawn , Quaternion.identity);
}
}
now this way we are sure that we got the position detailes before spawning the obejct; check this out and see if problem fixed and if not we countinue monitoring;
good luck;
The GetPosition() works perfectly fine. The position of the object when I spawn it with a key press like this: if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Alpha2)) { GetPosition(); Instantiate(Prefab1, _Position, Quaternion.identity); }
is fine Thank you for helping me already but the GetPosition() wasn't what I was having trouble with. I'm wondering how I activate the GetPosition(); Instantiate(Prefab, _Position, Quaternion.identity);
with a UI button. Do I add it to a button, do I replace the OnEnable with CubeButton, or AlphaButtonClick$$anonymous$$ask? like what do I do?