Question by
yashs662 · Apr 07, 2020 at 01:00 PM ·
c#infinite runner
help finding a gameobject from a instantiated prefab
hi I am making an infinite runner game in which the different segments of paths that are randomly spawned have predefined paths made by a path creator tool. I want to get the path gameobject from the instantiated prefab my spawner script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace PathCreation.Examples
{
public class Spawner : MonoBehaviour
{
public int count = 0, pathcount = 10;
public static int n = 0;
public static GameObject temppath;
[SerializeField] private Transform start;
[SerializeField] private List<GameObject> paths;
public GameObject player;
private Vector3 previous_path_position;
private void Awake()
{
previous_path_position = start.Find("endpoint").position;
}
private void Update()
{
while (n < pathcount)
{
Spawnpath();
n++;
}
if (count > 4 || count < -4)
{
count = 0;
}
print(n);
}
public void Spawnpath()
{
int rand = Random.Range(0, paths.Count);
Transform chosen_path = paths[rand].transform;
GameObject temppath = paths[rand];
if (count == -1 || count == 3)
{
Transform previous_path_transform = Spawnpath_left(chosen_path, previous_path_position);
previous_path_position = previous_path_transform.Find("endpoint").position;
}
if (count == 1 || count == -3)
{
Transform previous_path_transform = Spawnpath_right(chosen_path, previous_path_position);
previous_path_position = previous_path_transform.Find("endpoint").position;
}
if (count == 0 || count == 4 || count == -4)
{
Transform previous_path_transform = Spawnpath_straight(chosen_path, previous_path_position);
previous_path_position = previous_path_transform.Find("endpoint").position;
}
if (count == 2 || count == -2)
{
Transform previous_path_transform = Spawnpath_back(chosen_path, previous_path_position);
previous_path_position = previous_path_transform.Find("endpoint").position;
}
if (rand == 2)
{
count += 1;
}
if (rand == 3)
{
count -= 1;
}
}
public Transform Spawnpath_straight(Transform path, Vector3 spawnpoint)
{
Transform path_transform = Instantiate(path, spawnpoint, Quaternion.identity);
return path_transform;
}
public Transform Spawnpath_right(Transform path, Vector3 spawnpoint)
{
Transform path_transform = Instantiate(path, spawnpoint, Quaternion.Euler(0, 90, 0));
return path_transform;
}
public Transform Spawnpath_left(Transform path, Vector3 spawnpoint)
{
Transform path_transform = Instantiate(path, spawnpoint, Quaternion.Euler(0, -90, 0));
return path_transform;
}
public Transform Spawnpath_back(Transform path, Vector3 spawnpoint)
{
Transform path_transform = Instantiate(path, spawnpoint, Quaternion.Euler(0, 180, 0));
return path_transform;
}
}
}
my pathfollower script
using UnityEngine;
namespace PathCreation.Examples
{
// Moves along a path at constant speed.
// Depending on the end of path instruction, will either loop, reverse, or stop at the end of the path.
public class PathFollower : MonoBehaviour
{
public PathCreator pathCreator;
public PathCreator pathCreator1,pathCreator2,pathCreator3;
public EndOfPathInstruction endOfPathInstruction;
public float speed = 1;
float distanceTravelled;
public int lane = 2;
void Start() {
if (pathCreator != null)
{
// Subscribed to the pathUpdated event so that we're notified if the path changes during the game
pathCreator.pathUpdated += OnPathChanged;
}
}
void Update()
{
if ((Input.GetKeyDown(KeyCode.A)) && (lane > 1))
{
lane -= 1;
}
if ((Input.GetKeyDown(KeyCode.D)) && (lane < 3))
{
lane += 1;
}
if (lane == 1)
{
pathCreator = pathCreator1;
}
if (lane == 2)
{
pathCreator = pathCreator2;
}
if (lane == 3)
{
pathCreator = pathCreator3;
}
if (pathCreator != null)
{
distanceTravelled += speed * Time.deltaTime;
transform.position = pathCreator.path.GetPointAtDistance(distanceTravelled, endOfPathInstruction);
transform.rotation = pathCreator.path.GetRotationAtDistance(distanceTravelled, endOfPathInstruction);
}
}
// If the path changes during the game, update the distance travelled so that the follower's position on the new path
// is as close as possible to its position on the old path
void OnPathChanged() {
distanceTravelled = pathCreator.path.GetClosestDistanceAlongPath(transform.position);
}
}
}
I am using a temppath variable to store the current path being spawned and want to get a gameobject called path1 from it. plz help
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612230736im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Getting an Exception on Endless Runner 3D game 1 Answer
Rotate player automatically in an infinite runner to follow a winding road C# 1 Answer
if collider != trigger //do stuff 1 Answer
Set Y rotation on already spinning object 1 Answer
How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer