render line between 2 game objects

Hi, i made a little script that draws a line between 2 gameobjects. The problem is, that i am using if statements to look whether or not it is this or this object.

Now, i can only have Point 1 linked to Point 2, but i cant get Point 1 linked to Point 3. Because of the way i use the linerenderer etc.

This is my code:

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

public class ToolHandle : MonoBehaviour {
    Dictionary<string, StationPoints> stations = new Dictionary<string, StationPoints>();

    public LineRenderer _line;

    // Use this for initialization
    void Start () {

        _line = gameObject.GetComponent<LineRenderer>();

        StationPoints One = new StationPoints("One", false);
        StationPoints Two = new StationPoints("Two", false);
        StationPoints Three = new StationPoints("Three", false);
        StationPoints Four = new StationPoints("Four", false);

        stations.Add("One", One);
        stations.Add("Two", Two);
        stations.Add("Three", Three);
        stations.Add("Four", Four);

        Debug.Log(stations["One"].name);
        //stations["One"].linked = true;
        Debug.Log(stations["One"].linked);
    }
  
    // Update is called once per frame
    void Update () {

      

        if (Input.GetMouseButtonDown(0) && GameObject.Find("Wrench").active == true)
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
                if (hit.transform.name == "One")
                {
                    stations["One"].linked = true;
                    Debug.Log(stations["One"].linked);
                    _line.SetPosition(0, hit.transform.position);
                }
                if (hit.transform.name == "Two")
                {
                    stations["Two"].linked = true;
                    Debug.Log(stations["Two"].linked);
                    _line.SetPosition(1, hit.transform.position);
            }
        }
    }
}

and this is the stationpoints.cs

using UnityEngine;
using System.Collections;
using System;

public class StationPoints{
    public string name;
    public bool linked;

    public StationPoints(string newName, bool newLink)
    {
        name = newName;
        linked = newLink;
    }
}

How can i make that i am able to connect Point 1 to Point 3, or 4 or 5, and not only to point 2 ?

Thanks!

Hi,
It’s not clear what you’re trying to get in the end.To get a line drawn between point1 and point3 or point 1 and point4, just add more if statements.

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

public class ToolHandle : MonoBehaviour
{
    Dictionary<string, StationPoints> stations = new Dictionary<string, StationPoints>();

    public LineRenderer _line;

    // Use this for initialization
    void Start()
    {

        _line = gameObject.GetComponent<LineRenderer>();

        StationPoints One = new StationPoints("One", false);
        StationPoints Two = new StationPoints("Two", false);
        StationPoints Three = new StationPoints("Three", false);
        StationPoints Four = new StationPoints("Four", false);

        stations.Add("One", One);
        stations.Add("Two", Two);
        stations.Add("Three", Three);
        stations.Add("Four", Four);

        Debug.Log(stations["One"].name);
        //stations["One"].linked = true;
        Debug.Log(stations["One"].linked);
    }

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



        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
                if (hit.transform.name == "One")
                {
                    stations["One"].linked = true;
                    Debug.Log(stations["One"].linked);
                    _line.SetPosition(0, hit.transform.position);
                }
            if (hit.transform.name == "Two")
            {
                stations["Two"].linked = true;
                Debug.Log(stations["Two"].linked);
                _line.SetPosition(1, hit.transform.position);
            }
            if (hit.transform.name == "Three")
            {
                stations["Two"].linked = true;
                Debug.Log(stations["Three"].linked);
                _line.SetPosition(1, hit.transform.position);
            }
            if (hit.transform.name == "Four")
            {
                stations["Two"].linked = true;
                Debug.Log(stations["Four"].linked);
                _line.SetPosition(1, hit.transform.position);
            }
        }
    }
}

I would use a game tag. this way your having to add a hole bunch of if statements

void Update()
    {



        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
                if (hit.transform.tag == "LinePointTag")
                {
                    stations[hit.transform.name].linked = true;
                    Debug.Log(stations[hit.transform.name].linked);
                    _line.SetPosition(0, hit.transform.position);
                }
           
        }
    }

And you can use this if you want to build a line between multiple points, IE: point1 to point3, to point2, to point 5 and so on

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

public class ToolHandle : MonoBehaviour
{
    Dictionary<string, StationPoints> stations = new Dictionary<string, StationPoints>();

    public LineRenderer _line;
    public int TotalPoints = 0;

    // Use this for initialization
    void Start()
    {

        _line = gameObject.GetComponent<LineRenderer>();

        StationPoints One = new StationPoints("One", false);
        StationPoints Two = new StationPoints("Two", false);
        StationPoints Three = new StationPoints("Three", false);
        StationPoints Four = new StationPoints("Four", false);

        stations.Add("One", One);
        stations.Add("Two", Two);
        stations.Add("Three", Three);
        stations.Add("Four", Four);

        Debug.Log(stations["One"].name);
        //stations["One"].linked = true;
        Debug.Log(stations["One"].linked);
    }

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



        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit))
                if (hit.transform.tag == "LinePointTag")
                {
                    stations[hit.transform.name].linked = true;
                    Debug.Log(stations[hit.transform.name].linked);
                   
                    if(TotalPoints == 0)
                    {
                        _line.SetPosition(0, hit.transform.position);
                    }
                    else if (TotalPoints == 1)
                    {
                        _line.SetPosition(1, hit.transform.position);
                    }
                    else
                    {
                        _line.SetVertexCount(TotalPoints + 1);
                        _line.SetPosition(TotalPoints, hit.transform.position);
                    }
                   
                    TotalPoints ++;
                }
           
        }
    }
}

here is my unity scene

enjoy

2873478–210645–buildLine.unitypackage (7.18 KB)

Thanks! that works perfectly! I just have to figure out now, how to make it dynamic, because i want this cable/electric kind of thing, that people can connect stuff to it, no matter what point there is in between them. Just have no idea for the machine that needs power to check which station points are enabled AND that are linked to him