Trouble Swapping GameObjects on a Game Pad That are Prefabs.
Hi everyone, I am having a little trouble in making my gameobjects change from any position on my game pad.
I have a script that works great for gameobjects, however, it does not work in my game on items that are know as (Chips). The game acts like the script is not there and there are no error codes.
Here is the code that I am using to test this system out:
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Berry.Utils;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragSwitch : MonoBehaviour {
public const string DRAGGABLE_TAG = "Draggable";
private bool dragging = false;
private Vector2 originalPosition;
private Transform objectToDrag;
private Image objectToDragImage;
List<RaycastResult> hitObjects = new List<RaycastResult>();
#region Monobehaviour API
void Update ()
{
if (Input.GetMouseButtonDown(0))
{
objectToDrag = GetDraggableTransformUnderMouse();
if (objectToDrag != null)
{
dragging = true;
objectToDrag.SetAsLastSibling();
originalPosition = objectToDrag.position;
objectToDragImage = objectToDrag.GetComponent<Image>();
objectToDragImage.raycastTarget = false;
}
}
if (dragging)
{
objectToDrag.position = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
if (objectToDrag != null)
{
var objectToReplace = GetDraggableTransformUnderMouse();
if (objectToReplace != null)
{
objectToDrag.position = objectToReplace.position;
objectToReplace.position = originalPosition;
}
else
{
objectToDrag.position = originalPosition;
}
objectToDragImage.raycastTarget = true;
objectToDrag = null;
}
dragging = false;
}
}
private GameObject GetObjectUnderMouse()
{
var pointer = new PointerEventData(EventSystem.current);
pointer.position = Input.mousePosition;
EventSystem.current.RaycastAll(pointer, hitObjects);
if (hitObjects.Count <= 0) return null;
return hitObjects.First().gameObject;
}
private Transform GetDraggableTransformUnderMouse()
{
var clickedObject = GetObjectUnderMouse();
// get top level object hit
if (clickedObject != null && clickedObject.tag == DRAGGABLE_TAG)
{
return clickedObject.transform;
}
return null;
}
#endregion
}
Let me know if you have any ideas how to make gameobjects that are in prefabs and only shown during game play on a game pad. The script calls for the gameobject to be tagged, which I tagged them to the calling of Draggable, and nothing seems to work.
Your answer
Follow this Question
Related Questions
[Solved] Linear movement speed, reset position 1 Answer
Photon Multiplayer : position of client player is not getting synchronized. 0 Answers
SImple, but how do I check a game object's position in an If statement? 1 Answer
How to make an object move in the direction another object is facing on 2 axis? 1 Answer