- Home /
Turn single script into three seperate actions (mouse clicks)
I have 3 separate boxes (transforms) and a button that when clicked with the mouse checks to see if box one is empty? if it is... it places an object in the box, if box one if full it checks / places an object in box two... and box three and so on....
How can I make it so the checking & filling of each box is done by a separate mouse click (at the moment all boxes are checked / filled by a single click)
public GameObject newItem;
public Transform[] Slots;
public bool Slot1 = false; //false = empty, true = not empty
public bool Slot2 = false;
public bool Slot3 = false;
void OnMouseDown()
{
if(Slot1 == false)
{
Instantiate(newItem, Slots[0].position, Slots[0].rotation); //place item in 1st slot
Slot1 = true; //slot 1 now full
}
else if(Slot1 == true)
{
//Do Nothing !
}
if (Slot1 == true && Slot2 == false)
{
Instantiate(newItem, Slots[1].position, Slots[1].rotation); //place item in 2nd slot
Slot2 = true; //slot 2 now full
}
else if (Slot2 == true)
{
//Do Nothing !
}
if (Slot1 == true && Slot2 == true && Slot3 == false)
{
Instantiate(newItem, Slots[2].position, Slots[2].rotation); //place item in 3rd slot
Slot2 = true; //slot 3 now full
}
else if (Slot3 == true)
{
//Do Nothing !
}
}
All help greatly appreciated !
Answer by Cuttlas-U · Sep 27, 2017 at 06:45 PM
hi;
I give u a simple optimized example :
public GameObject newItem;
public Transform[] Slots;
public bool[] slotCheck; //false = empty, true = not empty
void OnMouseDown()
{
for (int i = 0; i < Slots.Length; i++)
{
if (slotCheck[i] == false)
{
Instantiate(newItem, Slots[i].position, Slots[i].rotation); //place item in 1st slot
slotCheck[i] = true;
break;
}
}
}
but your script is not well optimized u are not using any arrays so it makes your script complicated; i change the slot variable to be like an array ; so change its length in the editor just to be like Slots variable witch is transform;
then i created a loop and check for each one and used " break " so each time if it check it will break the function and dont check the others so u have to click another time;
its now seperates each spawning into a separate click but all spawnings are now at the same transform (not one at each in sequence)?
i forgot simple Slots[i] ; i edited the code now its ok ;
Answer by MaxGuernseyIII · Sep 27, 2017 at 07:03 PM
Okay. Your code does a little more than it seemed like your textual explanation stated. There is a dependency between slots. Some of those dependencies are redundant. Like Slot3 depends on both Slot2 and Slot1 but Slot2 already depends on Slot1.
Here's some code that I'm guessing fulfills your intent...
using UnityEngine;
public class Slot : MonoBehaviour
{
public GameObject NewItem;
public Slot[] Dependencies;
public Transform Container;
public bool Filled = false; //false = empty, true = not empty
void OnMouseDown()
{
foreach (var dependency in Dependencies)
if (!dependency.Filled) return;
if (Filled) return;
Instantiate(NewItem, Container.position, Container.rotation);
Filled = true;
}
}
You then configure it by dragging in the dependencies.
getting error
Assets/dealTypeThree.cs(27,12): error CS0246: The type or namespace name `Slot' could not be found. Are you missing an assembly reference?
I'm guessing that you did not copy the entire script and just took the contents out of it. If that's the case, you'll need to use whatever class name you've chosen ins$$anonymous$$d of Slot.
Your answer
![](https://koobas.hobune.stream/wayback/20220612135753im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
GUIText Instantiation and Update the Instantiated Text. C# 1 Answer
How to spawn gameobject from host to all clients 1 Answer
Destroying childs and Instantiate [C#] 0 Answers