• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
Question by ErikReichenbach · Mar 04, 2017 at 06:42 PM · scripting problemscriptingbasicsscriptableobject

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:

alt text

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:

alt text

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.

screen-shot-2017-03-04-at-13325-pm.png (62.3 kB)
screen-shot-2017-03-04-at-13344-pm.png (42.4 kB)
Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by AMU4u · Mar 04, 2017 at 06:52 PM

Try casting the variable as a GameObject, instead of a Transform.

Comment

People who like this

0 Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ErikReichenbach · Mar 04, 2017 at 07:21 PM 0
Share

It's looking for a type, not a property!

avatar image AMU4u ErikReichenbach · Mar 04, 2017 at 07:30 PM 0
Share

How about the instantiation script? I bet dollars to donuts you are casting it as something else.

avatar image ErikReichenbach AMU4u · Mar 04, 2017 at 07:38 PM 0
Share

Instantiation Script:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ReefWallSpawner : MonoBehaviour {
 
     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);
     }
 }

Show more comments
avatar image

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);
     }
 }
 
Comment

People who like this

0 Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AMU4u · Mar 04, 2017 at 08:30 PM 0
Share

GameObject clone = (GameObject)Instantiate (ReefWall1, SpawnPoints [spawnwallIndex].position, SpawnPoints [spawnwallIndex].rotation);

avatar image ErikReichenbach AMU4u · Mar 05, 2017 at 12:09 AM 0
Share

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 Movement Script on the new clones.

Again, your help is appreciated!

avatar image AMU4u ErikReichenbach · Mar 06, 2017 at 12:41 AM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

120 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

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

Help with a C# script .SetActive 1 Answer

How to add animations to character ? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges