Scripting Movement Fails for Instantiated Prefab
Hey everyone,
I am trying to instantiate a series of objects that move along a path (z-axis), then are destoryed at the end of the path. I have cobbled together all the elements, but am having trouble with the movement portion once the 2nd object generates.
Here is my movement Script (that works on the first prefab) :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReefObstacleMovement : MonoBehaviour {
public Transform target;
public float speed;
void Update() {
float step = speed * Time.deltaTime;
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
However, when I try to add a 'Target" to the prefab itself, it will not let me add a gameobject as a target in the inspector.
As an example, here is the 1st created pre-fab that is present when the scene starts that works ( notice I have "ReefMovementTarget" in the "Target" input field:
In contrast, here is the prefab itself, which is supposed to instantiate and move, but it only instantiates and does not move. I also cannot drag my game object "ReefMovementTarget" into the target input field, which is why I am guessing it is not moving:
Any help, advice, or workaround on why I cannot set a "target" for the prefab here would be ideal.
Thank you!
EDIT: Everything I have been researching has lead back to this link for Vector3 Move Towards, but with a public Transform, as opposed to a public GameObject, the prefab is not registering the "movement" script.
I would think this would be a simple case of replacing "transform" with "GameObject" but apparently it is more complex then that. Any advice here is appreciated.
Answer by AMU4u · Mar 04, 2017 at 06:52 PM
Try casting the variable as a GameObject, instead of a Transform.
How about the instantiation script? I bet dollars to donuts you are casting it as something else.
Instantiation Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReefWallSpawner : $$anonymous$$onoBehaviour {
public Transform[] SpawnPoints;
public float spawnTime = 3f;
public GameObject ReefWall1;
void Start () {
InvokeRepeating ("SpawnWall1", spawnTime, spawnTime);
}
void Update () {
}
void SpawnWall1()
{
int spawnwallIndex = Random.Range (0, SpawnPoints.Length);//set the index number of the array randomly
Instantiate (ReefWall1, SpawnPoints [spawnwallIndex].position, SpawnPoints [spawnwallIndex].rotation);
}
}
Answer by ErikReichenbach · Mar 04, 2017 at 08:12 PM
Destroy Script as well:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class DestroyScript : MonoBehaviour {
public float destroyTime = 3.0f;
void Start () {
Destroy (gameObject, destroyTime);
}
}
GameObject clone = (GameObject)Instantiate (ReefWall1, SpawnPoints [spawnwallIndex].position, SpawnPoints [spawnwallIndex].rotation);
Thank you for all the help, but I am having trouble understanding where you are adding these lines of code - Are they replacing the instantiation script portion on the Spawner Script entirely? I'm trying trial and error, but no seeing positive results yet.
I am beginning to think another script entirely is needed to address the "target" for the $$anonymous$$ovement Script on the new clones.
Again, your help is appreciated!
Hi, sorry I was rapid fire doing this.
Try replacing your instantiation line in your reefWall1 function to what I posted, which is;
GameObject clone = (GameObject)Instantiate (ReefWall1, SpawnPoints [spawnwallIndex].position, SpawnPoints [spawnwallIndex].rotation);
Something is going wrong with the way the object is instantiating most likely if the first prefab works fine. In this instance, we are using unity's new coding syntax when instantiating an object, by casting it directly using the brackets before the class (Instantiate).
Your answer
Follow this Question
Related Questions
Help with a C# script .SetActive 1 Answer
How to add animations to character ? 0 Answers
How can I temporary increase the size of an object? 2 Answers
How to make a boat move without controling it ? 0 Answers
can some one provide me a script? 1 Answer