Hello everyone ![]()
I am having a problem with my build script where my turrets are not being built where I want them.
It works perfectly when I call the build function from the same script, but once I call it from another script, the turret always gets build on Node 1.
I’m not really good at explaining, but hopefully you understand what I am trying to achieve ![]()
Here is the code where I Instantiate the turret:
using UnityEngine;
public class Node : MonoBehaviour {
public Color HoverColor;
private Renderer rend;
public Transform rendTransform;
private Color startColor;
private GameObject turret;
public Vector3 positionOffset;
void Start () {
rend = GetComponent<Renderer>();
rendTransform = GetComponent<Transform> ();
startColor = rend.material.color;
}
void OnMouseEnter()
{
rend.material.color = HoverColor;
}
void OnMouseExit()
{
rend.material.color = startColor;
}
void OnMouseDown()
{
BuildTurret ();
}
public void BuildTurret()
{
if (turret != null) {
Debug.Log ("Turret already build here.");
return;
}
GameObject turretToBuild = BuildManager.instance.GetTurretToBuild ();
turret = (GameObject)Instantiate (turretToBuild, rendTransform.transform.position + positionOffset, rendTransform.transform.rotation);
}
}
A node is a square box where the player is allowed to place a turret.
This code now places a turret where the cursor is. However, I don’t want to use mouse clicking as my building method. Instead I want to use a drag and drop system which I already have made.
This code is from another script which handles the dragging of UI elements to place turrets.
public void OnEndDrag (PointerEventData eventData)
{
transform.position = startPosition;
if (itemBeingDragged != null) {
if (itemBeingDragged.transform.tag == ("TurretTier1"))
{
itemBeingDragged.gameObject.SetActive (false);
HoldingTurretCard = false;
nodeScript.BuildTurret ();
}
} else
{
itemBeingDragged = null;
return;
}
}
When I’m doing this, the turret always gets built on node 1. Why?
Please ask if you want any more information. Sorry I’m bad at explaining, but I’m trying my best ![]()