replace GameObjects in game with other GameObjects using raycast detection.
So i made a mechanic where the player shoots a beam (raycastHit2D) and if it hits a certain object the object switches. at start i wanted to do it smart but... well not so smart lol. basically i wanted the object to switch to another object through a list of objects in order every time its hit by a raycast. didnt know how to do it so i did this:
public GameObject platform2;
public GameObject platform1;
public void Switch()
{
if (platform1.activeSelf == true)
{
platform2.SetActive(true);
platform1.SetActive(false);
}
else
{
platform2.SetActive(false);
platform1.SetActive(true);
}
and its called from the raycast script:
void UpdateLaser()
{
RaycastHit2D hit = Physics2D.Raycast(firePoint.position,firePoint.right);
if (hit)
{
lineRenderer.SetPosition(0, firePoint.position);
lineRenderer.SetPosition(1, hit.point);
Platform2Handler target = hit.transform.GetComponent<Platform2Handler>();
if(target != null)
{
target.Switch();
}
}
else
{
lineRenderer.SetPosition(0, firePoint.position);
lineRenderer.SetPosition(1, firePoint.position + firePoint.right * 100);
}
now the laser works great, the raycast is detecting the object correctly then executing the method target.Switch(); but i have to assign the object in the scene to the plaform1 and platform2 manually and i know can just instantiate a prefab of the object but i am not really worried about it atm but im more interested to know how to store these objects and cycle through them. i tried doing it with list and for loop. but it kept spawning them like crazy. funny thing as i am writing my issue here i keep finding more solutions haha.... well more like using duct tape but it works for my tiny project. any help is much appreciated. :D
Your answer
Follow this Question
Related Questions
How get a count of Lists assigned 0 Answers
How To Find GameObject From List And Add To Player List 0 Answers
Get a selected item from a GameObject list? 0 Answers
How can I destroy a Gameobject from a list at a specific index 1 Answer
What range of values need to be inserted if we need to pull a random item from the list? 0 Answers