Hi everyone, I make a code who want create a copy of gameObject similar to one after maked and posicionated; but this only create the copy ignoring the condicion of have dragged one in the panel.
The code is:
using System.Reflection;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragInfoC : MonoBehaviour
{
public Rigidbody WC;
public GameObject PanelW;
private Transform WCPrefab;
public Image containerImage;
void Start()
{
DropMe DropMe = GetComponent<DropMe>();
if (containerImage != null)
PanelW = GameObject.FindGameObjectWithTag("C");
Rigidbody WC = Instantiate(this.WC) as Rigidbody;
}
void Update()
{
DropMe DropMe = GetComponent<DropMe>();
Debug.Log("Its working!!!");
}
My actual project consist in a interface who the player drag items from a top panel to another panel who contenis diferents possible locations (diferent cells) for this gameObject. I distributed copys of the gameObject in the desired places.
I would appreciate if you check that it is wrong in the code; I’m big noob, I know
And excuse my English, I’m Spanish speeker. Thanks!
I did not understand what you said, sorry.
Could you try to explain it a bit, again?
Right now, your code is creating a copy of the rigidbody right away when it starts. Is that what you want?
I think you should get rid of the Update() function (if that’s all there is to it). If you explain what you’re trying to do in Update(), maybe a suggestion could be offered as to how to improve it, because currently it doesn’t look very good (like sensible… it’s looking for the component every frame, which is overkill, I imagine).
Hi! Now I make some changes in the code, with probables mistakes, cause this still not working.
using System.Reflection;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
public class DragInfoC : MonoBehaviour
{
//public Rigidbody WC; (here I try in vane in the past)
public GameObject PanelW;
private GameObject WC1Prefab;
private DropMe dropMe;
public Image receivingImage;
private Object CW;
void Start()
{
DropMe dropMe = GetComponent<DropMe>();
WC1Prefab = Resources.Load("CW") as GameObject;
}
void Update()
{
}
void CreateGameObject()
{
dropMe.GetDropSprite();
if (receivingImage == null);
else PanelW = GameObject.FindGameObjectWithTag("C");
gameObject.SetActive(true);
}
}
@methos5k Thanks for your answer! I explain you best:
I want use this code to instantate an gameObject when the user drag one pic from the top-draggeable panel to another cell of the other panel(“reciving”), who should contain the script and the prefab.
To explain it better, I’ll tell you that after the instantate, the user clicks a button that deactivates the mentioned panels leaving to see the respective gameObjects wanted. Also, you must know that I “make diferents” the cells of the top panel through the use of custom tags.
Besides, i want assign multiple gameObjects to every cell of the “reciving” panel and make visible the type of ths who correspond to the mencioned tag.
In spite of everything, maybe instantate it may not be the best possible action, but I can’t understand right now another way to make active the gameObjects.
Alright – I did not understand everything, again, this time, but I have a better idea.
First of all, I think you should use the IPointerDrag and IDrop interfaces. This will let you move things well. Do you already have that in some other code? Like, how do you drag the pic to drop on another panel?
Then, whether you’re using those already or do set them up going forward, you can have the OnDrop method check the item (that was dropped in there) and proceed to create whichever gameObject you wish at that spot (if it’s a prefab, it will also include the script on it when you instantiate it).