Boolean array with names

So im trying to link points with each other, the points that have to be linked are actual objects. At this moment i use IF and IF ELSE statements that look to the name of the point, and just some booleans for each point setting to false. Now that is not ideal because when a player places one point, i have to give it a name with a script.Maybe using a boolean array?I just want them to be able to place down linkpoints and check with my ‘‘ToolScript’’ to see what tag they have and link 2 together.

This is my script now:

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class LinkTool : MonoBehaviour
{

    //public GameObject node;

    bool point1 = false;
    bool point2 = false;
    bool point3 = false;

    float range = 10f;
    //public bool[] Points = new bool[10];

    //Detect if player is in range
    //If in range and looking at object
    //use Left mousebutton to activate Point1
    //and left mousebutton to activate and link Point1 to point2
    //if linked show the Indicator

    GameObject hand;

    // Use this for initialization
    void Start()
    {

    }

    private void Awake()
    {

    }

    // Update is called once per frame
    void Update()
    {

        //Do raycast that originates from the mouse clicked position,
        if (Input.GetMouseButton(0))
        {

            RaycastHit hit;
            Ray rayOrigin = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(rayOrigin, out hit, range))
            {
                if(hit.collider.name == "Point1")
                {
                    point1 = true;

                    Debug.Log(point1);
                }else if(hit.collider.name == "Point2")
                {
                    point2 = true;
                }else if(hit.collider.name == "Point3")
                {
                    point3 = true;
                }
            }


        }

        if(point1 == true && point2 == true){
            Debug.Log("Linked!");
        }

    }

}

Thanks in advance!

not too sure as to what you are trying to build, but if you look at something like node maps for navigation (which are basically points and links) the links are held within each node.

public class Node 
{
public List<Node> connectedNodes;
}

The ‘‘LinkPoints’’ are actual objects haha

that doesn’t really change the concept… the connected “things” are held in the list

Well using Node gives me an error, so i think u mean

List<string> pointsList = new List<string>();?

How would i make the raycast know which object i clicked?

Basicly; people can put down these points, and use left click to link 2 points together. And when they have done that, a object has to appear. That is what i want to achieve

quick and easy solution
simply use monobehaviours OnMouseDown method, nodes must have a collider

robust, stable, and powerful solution
you can use Unity’s EventSystem Handlers to link up objects, while being cross-platform compatible.
you could use two left clicks… but maybe dragging makes more sense (so that you don’t accidentally connect node BtoC when you simply wanted to connect CtoA for example)

Using BeginDrag, Drag, and Drop handlers you can have clean support for dragging to both nodes. you can also instantiate a line renderer that links to the first node and then to your cursor, switching to the other node when you drop onto the second node

If only there was a tutorial for that second option haha!

BoredMormon has a tutorial about it on Youtube

1 Like

Hey @luckie12 ! You should try using a Dictionary :slight_smile: They are built for things such as this

Dictionary<string, bool> myBools = new Dictionary<string, bool>();

myBools["bool1"] = true;
myBools["bool2"] = true;

string aBoolName = "bool1";

if(myBools[aBoolName]){
  //aBoolName (bool1) is true!
}
1 Like