I am working on 2 lampposts and 1 generator, now what i want is, when i click left on the lamppost and then left click on the generator, they get connected. This works with 1 lamppost, this does not work if i have a second lamppost. And the reason is because i am using statis indexes. This is my script,
if (Input.GetMouseButton(0))
{
if (Physics.Raycast(ray, out hit, 100) && hit.transform.gameObject.layer == 8)
{
//Add new pole, name, Connected = false, Selected = true
poles.Add(new Poles(hit.transform.name, false, true));
}
if (hit.transform.gameObject.layer == 9 && poles[0].isSelected == true)
{
//Set connected to true
//Set selected back to false
poles[0].connected = true;
poles[0].isSelected = false;
//Debug the outcome
Debug.Log("Pole " + poles[0].lantName + ", connected = " + poles[0].connected + ", selected = " + poles[0].isSelected);
}
}
As you can see, im using poles[0] but the 0 should be dynamic, how can i achieve this for EVERY lamppost i am going to put in the scene?
Also, unlinking, how would i remove it from the list, by removeAt ?
Instead of using 0, just use poles[poles.count-1] to get the last pole added.
However, doesn’t appear you are checking if a pole is in your list already. For removing, you’ll just need to find the correct object. If you can’t do a list of gameobjects, you’ll have to either loop through your list (if the name is unique per pole) or use a .Find or some other method to find the pole.
If I understand what brathnann is trying to tell you about remove, you’ll need to find the pool object by gameObject name.
So loop through your array of poles objects using a for loop. At each iteration of the for loop check for the name of the object returned by the RayCast and if found use poles.RemoveAt()
for(var i = 0; i++; i < poles.length)
{
if(poles.lantname == hit.transform.name)
{
poles.RemoveAt(i);
}
}
Thankyou guys, for explaining me, i will try this after i rewrote the whole script.
I found out that im not doing this on the best ways.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tool : MonoBehaviour {
List<Poles> poles = new List<Poles>();
void Update()
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
var polesCount = poles.Count - 1;
if (Input.GetMouseButton(0))
{
if (Physics.Raycast(ray, out hit, 100) && hit.transform.gameObject.layer == 8)
{
//Add new pole, name, Connected = false, Selected = true
poles.Add(new Poles(hit.transform.name, false, true, 0));
}
if (hit.transform.gameObject.layer == 9 && poles[polesCount].isSelected == true)
{
poles[polesCount].connected = true;
poles[polesCount].isSelected = false;
poles[polesCount].hasPower = 0;
Debug.Log("Pole " + poles[polesCount].lantName + ", connected = " + poles[polesCount].connected + ", selected = " + poles[polesCount].isSelected + ", HasPower = " + poles[polesCount].hasPower);
}
else
{
Debug.Log("Nothing is selected!");
}
} else if (Input.GetMouseButton(1))
{
if (Physics.Raycast(ray, out hit, 100) && hit.transform.gameObject.layer == 8)
{
Debug.Log("Removed Pole");
Debug.Log(poles[0]);
}
}
}
public int GeneratorHasEnoughFuel()
{
int fuel = 1000;
if (fuel >= 500)
{
return fuel;
}
return 0;
}
void StartGenerator()
{
if (GeneratorHasEnoughFuel() >= 100)
{
//ON
}
else
{
Debug.Log("Generator Does Not Have Enough Fuel!");
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Poles : MonoBehaviour {
public string lantName;
public bool connected = false;
public bool isSelected = false;
public int hasPower = 0;
public Poles(string newLantName, bool newConnect, bool newSelected, int newHasPower)
{
lantName = newLantName;
connected = newConnect;
isSelected = newSelected;
hasPower = newHasPower;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Generator : MonoBehaviour {
public int fuel;
public bool isOn = false;
public Generator(int newFuel, bool newIsOn)
{
fuel = newFuel;
isOn = newIsOn;
}
}