- Home /
how to set a variable in prefabs or use an object in the hierarchy/prefab section in my script
So, i have a spawner script and a waypoint movement script (Spawner.cs and WayPointAI.cs)
Spawner -
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public Transform spawnPoint;
public Transform spawnObject;
public int spawnTotal;
public float timeBetweenSpawns;
// Use this for initialization
void Start ()
{
StartCoroutine (SpawnGameObject());
}
// Update is called once per frame
void Update ()
{
}
IEnumerator SpawnGameObject ()
{
for( var x = 0; x < spawnTotal; x++)
{
Instantiate(spawnObject, spawnPoint.position, spawnPoint.rotation);
yield return new WaitForSeconds(timeBetweenSpawns);
}
}
}
My spawner script will spawn a NPC prefab on the spawner object.
Waypoint movement script -
// Use this for initialization
void Start ()
{
//find each waypoint object in the scene when game is loaded
wayPoint [0] = GameObject.Find("WayPoint 1").transform;
}
// Update is called once per frame
void Update ()
{
// once npc reaches last waypoint, it is destroyed
if(currentWayPoint == 1)
{
Destroy (this.gameObject);
}
else
{
walk();
}
}
//makes npc move
void walk()
{
//facing direction
//npc looks at next waypoint
Quaternion rotation = Quaternion.LookRotation(wayPoint[currentWayPoint].position - transform.position);
//how fast npc turns
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime*rotationSpeed);
//movement
Vector3 wayPointDirection = wayPoint[currentWayPoint].position - transform.position;
float speedElement = Vector3.Dot(wayPointDirection.normalized, transform.forward);
float speed = accelerate * speedElement;
transform.Translate(0,0,Time.deltaTime*speed);
}
void OnTriggerEnter(Collider collider)
{
if (collider.tag == "WayPoint 1")
Destroy (this.gameObject);
//currentWayPoint = 1;
//currentWayPoint++;
}
}
After Spawner.cs pulls an NPC from the prefabs and then spawns it on the Spawner object, WayPointAI will move the NPC fromt he spawner, to WayPoint 1 (object in the hierarchy). once the NPC reaches WayPoint 1, it despawns (is destroyed) the problem here is that i can't set the WayPoint element 0 to an object when it's in the prefab folder (when i drag the WayPoint1 object from the hierarchy onto the WayPoint element 0 variable in the NPC's WayPointAI script (the npc is in the hierarchy), i get an error sign and it doesn't let me set the variable to the object in the hierarchy. you may think "why is this a problem? the NPC is set to walk to an object named 'WayPoint 1', so why do you need to set the variable to the WayPoint 1 object too?". That's a good question. the reason i want to be able to set the variable is because i want to make multiple spawners that go to their own WayPoint 1's. so there's two ways i can solve this problem: first, i could make it so i can set the variable on the NPC's WayPointAI script in the prefabs folder and everything will be golden, or i can waste time and space by making another 50 identical scripts with the two lines with "WayPoint 1" in them to "WayPoint 2" and "WayPoint 3", and so on. but i would really prefer it if i could just set a variable on the NPC in the prefab folder. does anyone know how? help would be greatly appreciated! and if you're confused on soemthing just ask me and i'll try to be more specific and detailed.
Thanks in advance!
first, i could make it so i can set the variable on the NPC's WayPointAI script in the prefabs folder and everything will be golden
But then you'd have to make 50 of these prefabs with different waypoints assigned to them and somehow select which one to spawn every time, right?
The objects in hierarchy/scene are actual existing object instances. The objects in project view/prefabs are just blueprints for making objects. It doesn't make sense that a blueprint of something would reference a real world object (e.g. this is the blueprint for building a house... but only for a certain Jack J. Johnson in Wales). What if you'd spawn an object like this to a different scene that doesn't have the objects it references (waypoints) ?
This is the fundamental problem you are having.
So depending on what exactly you want, I think you'll have to come up with an "AI" that selects the waypoint for each NPC in their Start() method based on some rules.
public Transform[] wayPoint = new Transform[1]; //create an array for storing each waypoint
it's public, could it be anything else?
Answer by Baste · Mar 18, 2015 at 07:48 AM
Put the waypoint(s) on the spawner, and drag and drop them from the hireachy.
Then, when you spawn a NPC, pass the waypoints to it:
for( var x = 0; x < spawnTotal; x++)
{
GameObject spawned = Instantiate(spawnObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
WaypointMovement mover = spawned.GetComponent<WaypointMovement>();
mover.waypoints = waypoints;
yield return new WaitForSeconds(timeBetweenSpawns);
}
You should also try to get rid of all string comparrison and GameObject.Find - they're incredibly inflexible, as any tiny misspelling (like an extra space) will break your code. GameObject.Find is also really slow.
i'm a little confused, so i could reference my waypointmovement script in my spawner script and then it would look like this?
for( var x = 0; x < spawnTotal; x++)
{
GameObject spawned = Instantiate(spawnObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
Waypoint$$anonymous$$ovement walk = spawned.GetComponent<Waypoint$$anonymous$$ovement>();
walk.wayPoint = wayPoint;
yield return new WaitForSeconds(timeBetweenSpawns);
}
i get an error on line 5 saying "the name 'wayPoint' does not exit exist in the current context" why would it say that? and i looked some stuff up about how to replace my string comparisons and gameobject.find's and people said to pass the object reference, is there any scripting API where i could learn what that is? i couldn't find any.
Is wayPoint public or private? If it's private and you're trying to access it from Spawner, you'll get that error.
You need to actually have a public waypoint array on your spawner, and send that to the walker:
public class Spawner : $$anonymous$$onoBehaviour {
public Transform[] waypoint; //populate this from the inspector
...
IEnumerator SpawnGameObject () {
for( var x = 0; x < spawnTotal; x++) {
GameObject spawned = Instantiate(spawnObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
Waypoint$$anonymous$$ovement walk = spawned.GetComponent<Waypoint$$anonymous$$ovement>();
walk .wayPoint = wayPoint;
yield return new WaitForSeconds(timeBetweenSpawns);
}
}
}
Answer by aLovedHater · Mar 20, 2015 at 11:30 PM
so my spawner currently looks like this -
using UnityEngine;
using System.Collections;
public class Spawner : MonoBehaviour
{
public Transform spawnPoint;
public Transform spawnObject;
public int spawnTotal;
public float timeBetweenSpawns;
private WayPointAI wayPointAI;
public Transform[] waypoint;
// Use this for initialization
void Start ()
{
StartCoroutine (SpawnGameObject());
}
// Update is called once per frame
void Update ()
{
}
IEnumerator SpawnGameObject ()
{
for( var x = 0; x < spawnTotal; x++)
{
GameObject spawned = Instantiate(spawnObject, spawnPoint.position, spawnPoint.rotation) as GameObject;
WayPointAI walk = spawned.GetComponent<WayPointAI>();
walk.wayPoint = waypoint;
yield return new WaitForSeconds(timeBetweenSpawns);
}
}
}
in my hierarchy i have Spawner's waypoint variable set to the waypoint, (array size of 1) and Waypoint movement's waypoint variable is set to the waypoint as well (also with an array size of 1). everything runs fine but when i run it, it will spawn in one NPC that doesn't move and i'll get the error "NullReferenceException: Object not set to an instance of an object, Spawner+c__Iterator0.MoveNext ()"
and it will point me to this line of code -
WayPointAI walk = spawned.GetComponent<WayPointAI>();
Your answer
Follow this Question
Related Questions
error CS0161: not all code paths return a value 2 Answers
prefabs and IDs 1 Answer
scripting error 1 Answer