- Home /
How to check a sequence of gameobjects and tell if it was correct or not?
Hey everyone! Lately I came across an idea I couldn't realize on my own yet, sadly. So in my little game you have to redraw pictures of symbols, which have several so-called tracing points (2D polygone collider) placed on important points of the symbols. Ingame the mouse can draw over these symbol-templates and the name of the overdrawn tracing point will appear in the console log. So far so good. Now the last complicate step is to add the functionality of checking the tracing points, if they actually got overdrawn in the right sequence. Right now I have no idea how to realize that and hope someone kinda understand what I'm talking about and how a solution could look like.
Every symbol is a simple gameobject with 2D polygone collider and the image with the template on it. A second gameobject is the symbols child in the hierachy, which has the tracing points as children of the child. The tracing points have 2D ploygone collider, as already said above. This is how the script for the tracing points looks like for the debug.log right now:
using UnityEngine;
using System.Collections;
public class TracingPoint : MonoBehaviour {
void OnMouseEnter (){
if (Input.GetMouseButton (0)) {
Debug.Log (name);
}
}
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
I thought about giving the first child a script for an gameobject array. Then I could drag and drop the children of it in there in the right order ..sadly I have no idea if this thought is even near of a possible solution @_@ ..just hope someone can help somehow.
Thank you in advance!!! ^ ^ Btw ..if somethings is not clear pls ask!
Check the solution I gave here:
https://answers.unity.com/questions/1479333/how-do-you-check-if-an-instance-of-an-object-has-b.html
The principle is the same except the objects had to be clicked one after the other, but I am sur you can easily tweak the scripts to handle mouse over.
Answer by MonkeyHood · Jun 24, 2018 at 03:26 AM
using UnityEngine;
public class TracingPoint : MonoBehaviour
{
//This is a number representing when this TracingPoint should be pressed
//First, second third, etc
public int OrderId;
//Set this in the inspector
public TracingPointOrderValidator Validator;
void OnMouseEnter()
{
if (Input.GetMouseButton(0))
{
Debug.Log(name);
Validator.TracingPointPressed(OrderId);
}
}
// Use this for initialization
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
This script will check if the tracing points were pressedin order.
using System.Collections.Generic;
using UnityEngine;
public class TracingPointOrderValidator : MonoBehaviour
{
public TracingPoint[] TracingPoints;
private List<int> pointsPressedOrder = new List<int>();
public void TracingPointPressed(int pointId)
{
pointsPressedOrder.Add(pointId);
}
//Call this when you want to validate the order of the points
public bool ValidatePressedOrder()
{
//check that we have
bool pointsInOrder = true;
//check that we have pressed each Tracing point, otherwise it can't possibly be in order
if (TracingPoints.Length == pointsPressedOrder.Count)
{
for (int i = 1; i < pointsPressedOrder.Count; ++i)
{
if (pointsPressedOrder[i] != i)
{
pointsInOrder = false;
break;
}
}
}
else
{
pointsInOrder = false;
}
Debug.Log("Valid order: " + pointsInOrder);
return pointsInOrder;
}
}
Wow ..I'm without words. As far as I understand the scripts, it's exacly that what I tried to imagine of! ^ ^ Well, the console don't want to spit out the message, if it's actually the right order, but I'm pretty sure I will get it to work. Thank you very much for your help, I appreciated it! :)
Answer by hectorux · Jun 23, 2018 at 11:35 AM
You will always have to set the proper order by ordering all in hierarchy and using the transform.GetComponentInChildren wich will give you all the transform in the hierarchy order. Or making an array in order(both the same, the transform more automatic)
I have all tracingpoints in proper order and I kinda unterstand the transform.GetComponentInChildren, but I just cannot transfer it to my idea/problem, sry. For everything I programmed, yet, I always had to rely on tutorials, because I don't understand most of the program$$anonymous$$g-stuff, sadly. I appreciate your comment, :) but I guess I need a script as example. :(