Hi. I am /very/ new with Unity and C#. Thankfully I have very helpful friends who are able to help me learn, but unfortunately they know C# well, but not it’s integration with Unity and I’m having a very difficult time.
I’m trying to create a cocktail mixing mechanic where you would drag an ingredient onto a cocktail shaker. (The following is weird and esoteric and the result of having a few people with varying skill levels help me separately.) Each ingredient is a GameObject with an int attached to it and supposedly I have a set of enums that name all of my objects and their ints and a recipe list … and I need the list to know what’s being dropped into it and how many things are dropped into it.
The “what” being dropped needs to be checked against the recipes and the “how many” (which right now is “drinks.Count”) I need to figure out how to send that number to an animator/animation so that a lit indicator will show how many ingredients are in the shaker at one time.
Then… upon hitting a button that says “mix”, I need it to do the check and then either come up with a correct drink or to return a null value.
I have no idea how to get it to display the sprite of the finished result, presently.
Could anyone help me piece everything i have together?
This is my DropMe script that goes on the shaker:
(It’s long, due to the amount of drinks I have, so I don’t want to paste it straight here and make things hard to read)
There’s presently one error in the script on line 141 saying that “DrinkExtensions” is a nested class, and I have no idea what that means.
And at the end, I wanted to try seeing if the recipe thing would actually work, so I was trying to set up a Debug.log thing for the console.
This is… complicated, and I’m in way over my head and I would very much appreciate any help that you can give me.
Hey there. Sounds like a fun & cool endeavour. I’m glad that you have help with it. I’m all for assisting ya, but perhaps we could break this down…
Getting the list to know what’s dropped into it.
Are you using IDropHandler? Check that the item can go there, and then read its value from that item?
lol. A whole system in a question is a lot. I can’t speak for other people, but for me it’s easier to tackle 1 issue at a time…
Thank you!
I’ve been doing a little more finangling since posting this. I’ve moved Drinks, DrinkExtensions, Recipe and DrinksMenu out of the DropMe script and we’re trying to figure out the nested class issue.
So, yeah. I have an IDropHandler on the DropMe script. To read the value from the item, I’ve got these…
public void OnDrop(PointerEventData data)
{
int? dropId = GetDropId(data);
if (dropId.HasValue)
{
drinks.Add((Drink)dropId.Value);
// drinks.Count is now the thing you somehow need to get into your animation thingy
}
}
private int GetDropId(PointerEventData data)
{
var originalObj = data.pointerDrag;
if (originalObj == null)
return null;
var dragMe = originalObj.GetComponent<DragMe>();
if (dragMe == null)
return null;
return dragMe.id;
}
Just look up setting variables in the animator. I’m not confident to tell you I know the right thing to write, but here’s a quick link to the docs:) Unity - Scripting API: Animator.SetInteger
Your stuff looks good, btw.
Feel free to update your post if/when you have (another) specific issue.
Thanks! We’re… getting there.
So, I just basically got the animator to work on another object by setting triggers using a button. (It took me forever to figure out) but I don’t know how to send it a variable.
Is there a way that you know of that I can see how the button in the inspector’s code works? Since I don’t know the syntax very well, I would appreciate being able to see how exactly something like this looks in script form:
Then I could try to figure out how to send a message to something else, I guess? which is a big problem right now.
Also, here’s the script that I’ve got for my button right now.
using System.Reflection;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using System.ComponentModel;
using System.Collections.Generic;
public class MixButtonScript : MonoBehaviour
{
public void OnPointerClick(PointerEventData data)
{
Shaker.Instance.Mix();
}
public void Mix()
{
Debug.Log("Mixing " + string.Join (", ", Ingredients));
Drinks? result = DrinkMenu.Mix(Ingredients);
if (result.HasValue) {
Debug.Log("You made " + result.Value.GetName ());
} else {
Debug.Log ("You didn't make anything.");
}
// Empty the ingredients after mixing them
Ingredients.Clear();
// probably also update the animation here?
}
}
It doesn’t know what ‘ingredients’ and ‘DrinkMenu’ are right now. How can I make it so that it will get the information from the script that has those?
Okay, the Button OnClick() is a UnityEvent (you can look into that :))
As for setting the values, did you read the link I posted in my previous message?
I think your 'DropMe" script is that whole file you linked/pasted in the first post?
That may even be why you got the error about the nested class. It’s also maybe(/probably?) why it can’t find DrinkMenu, because DrinkMenu is inside a DropMe class.
More follow up later.
Yes, I get the UnityEvents and the EventSystem. I suppose I worded it wrong.
What I want to know is basically how to send messages to other objects and I used the button as an example.
So for instance, when an object is dropped onto the Shaker object, it has this:
public void OnDrop(PointerEventData data)
{
int? dropId = GetDropId(data);
if (dropId.HasValue)
{
Shaker.Instance.AddDrink((Drinks)dropId.Value);
// drinks.Count is now the thing you somehow need to get into your animation thingy
}
}
And in my Shaker, I have
public static Shaker Instance { get; private set; }
private List<Drinks> Ingredients = new List<Drinks>();
void Awake()
{
Instance = this;
}
public void AddDrink(Drinks drink)
{
Ingredients.Add(drink);
// update the animation or whatever here, because you added a new drink
}
What I’d love to find out from the button is how to say, basically “This value is x, update it here” with “here” being the animator controller for my indicator, right? The thing is, I don’t know how to say that.
Yes, I did look at the link you posted. I still have it up. I’m just not exactly sure how I get it to reference a specific object.
The problem with the nested classes is fixed. It’s precisely as you said. To fix it, I made a separate script with the Drinks, DrinkExtensions, Recipe and DrinkMenu classes and that just sort of exists on its own right now. So I’m trying to figure out how to reference a list from another script.
I suppose that my question right now is how do I get it so that when the result of the recipe happens, it takes the enum and its associated GameObject and creates the sprite on screen.
Or at the very least, how do I write a proper debug.log so that I can at least see whether it’s doing its results thing correctly?
k, first off. I’m glad you got the code fixed up a bit so it’s working a little nicer. Next, I admit to being a little “lost” when following your explanations - just a little. For a “proper debug.log” (You can short hand that to ‘print’, usually), is just do the debug.log and fill in whatever information you’re hoping to see. As for the animator targetting a specific object, i would have thought the animator is on the object, and you’re passing the value of your result…
I feel like my answer is becoming somewhat vague.
Since your code has been updated, maybe you can snip a sort of example out and-repose the question, using a small section here that I/anyone else can look over to help further