- Home /
Question by
bsf9 · Jul 22, 2018 at 08:56 PM ·
unity 5instantiatemouseposition
Instantiate a button at mouse click position,
Hi,
I am trying to instantiate a button at the end of line. The line shows up perfectly but the when I instantiate the button, nothing appears. Any help as to what I am doing wrong is much appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LineDraw : MonoBehaviour {
private new Camera camera;
private Vector3? lineStartPoint = null;
public Material lineMaterial;
private float width = 0.01f;
public GameObject button;
void Start () {
camera = GetComponent<Camera>();
}
void Update () {
if (Input.GetMouseButtonDown(0))
{
lineStartPoint = GetMouseCameraPoint();
}
else if (Input.GetMouseButtonUp(0))
{
if (!lineStartPoint.HasValue)
{
return;
}
// draw line
Vector3 lineEndPoint = GetMouseCameraPoint();
var gameObject = new GameObject();
var lineRenderer = gameObject.AddComponent<LineRenderer>();
lineRenderer.material = lineMaterial;
lineRenderer.SetPositions(new Vector3[] { lineStartPoint.Value, lineEndPoint });
lineRenderer.startWidth = width;
lineRenderer.endWidth = width;
// THE FOLLOWING TWO LINES DO NOT APPEAR TO BE WORKING
GameObject newGameObj = Instantiate(button, GetMouseCameraPoint(), Quaternion.identity) as GameObject;
Button newButton = newGameObj.GetComponent<Button>();
lineStartPoint = null;
}
}
// get the current mouse position in world coordinates
private Vector3 GetMouseCameraPoint()
{
var ray = camera.ScreenPointToRay(Input.mousePosition);
return ray.origin;
}
}
Comment
Your answer
Follow this Question
Related Questions
instantiate spawning meany objects in multiplayer 0 Answers
Instantiate at mouseposition problem 3 Answers
Object Instantiate 1 Answer
Create random order and go through till the end (C#) 1 Answer