Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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 /
avatar image
0
Question by Erin abrams · May 14, 2015 at 04:04 PM · c#instantiaterandom

why wont the instance of the object move after its been instantiated.

Hello, I was just wondering if you could proof read this script and tell me why my instance of an object wont move after its been instantated. It is supposed to spawn a clone of a cube in a specified position every second and move in the forward direction. Afterwards it will delete after a specified time. It's doing everything right except it's not moving forward.I have all the values and an object set in the inspector. I have this script on an empty game object and I have a gameobject that it will make instances of. Thanks.  Script:

 using UnityEngine;
 using System.Collections;

 public class Enemy : MonoBehaviour {

 public GameObject enemy;
 public float SpawnSpeed = 1f;
 public float destroyTime;
 public float MoveSpeed;
 Transform MyTransform;

 void Start () {
     MyTransform = transform;
     InvokeRepeating ("Spawn", SpawnSpeed, SpawnSpeed);
 }

 void Update () {
     MyTransform.Translate  (Vector3.forward * MoveSpeed * Time.deltaTime);
 }

 void Spawn(){
     Vector3 spawnLocation = new Vector3 (Random.Range (-5, 5), 1f, 0f);
     GameObject clone = (GameObject)Instantiate (enemy, spawnLocation, Quaternion.identity);
     Destroy (clone,destroyTime);
 }
 }
Comment
Add comment · Show 2
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 fafase · May 14, 2015 at 06:23 PM 0
Share

This code is moving the object with the script.

The enemy may have a script for movement but this is not affecting it.

avatar image Teh_ouj · May 15, 2015 at 01:05 PM 0
Share

How about you create another sccript that just moves the enemy forward. this script should just instantiate the enemy. enemy instantiated from a prefab with a script attached to it. after instantiating the enemy will run itsself and destroy itself.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by LostInCode404 · May 15, 2015 at 03:08 AM

I think you are moving the object which is spawning the enemy and not the enemy itself thats why the object the script attached to is moving by transform.translate. To actually move the enemy you have to write clone.transforn.translate below instantiate line and above the deatroy line. Hope it helps. Ask me if you have sone doubts

Comment
Add comment · Show 8 · 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 fafase · May 15, 2015 at 08:58 AM 0
Share

Nope, if he does that the object will only move that one time.

What he needs is create a new script, copy paste the Update from that script to the new script and remove it from this one.

Then the object will move.

avatar image Erin abrams · May 15, 2015 at 10:43 AM 0
Share

Thanks guys, once I get on my computer I will be sure to try it.

avatar image Erin abrams · May 15, 2015 at 11:40 AM 0
Share

I tried both of these and they both didn't work.

avatar image LostInCode404 · May 15, 2015 at 11:41 AM 0
Share

Can you tell us the script you wrote . so i can probably guess the problem

avatar image Erin abrams · May 15, 2015 at 11:48 AM 0
Share

using UnityEngine; using System.Collections;

public class othe : $$anonymous$$onoBehaviour {

 Transform $$anonymous$$yTransform;
 public float $$anonymous$$oveSpeed;
 void Start () {
     $$anonymous$$yTransform = transform;
 
 }
 
 // Update is called once per frame
 void Update () {
     $$anonymous$$yTransform.Translate (Vector3.forward * $$anonymous$$oveSpeed * Time.deltaTime);
 
 }

}

Show more comments
avatar image
0

Answer by Teh_ouj · May 15, 2015 at 01:33 PM

The code is improperly written. just take the part inside the update ,as @fafase said, and put it in the update of a seperate script. Also put the destroy function in the start of the new script. attach that script to enemy and make the whole thing as a prefab. Prefab it by dragging your enemy from hierarchy to the project section. then its a prefab. This script should only intantiate. here is what it should look like:

SpawnEnemyScript:

public GameObject enemy;

public float SpawnSpeed = 1f;

void Start () {

  InvokeRepeating ("Spawn", SpawnSpeed, SpawnSpeed);

}

void Spawn(){

  Vector3 spawnLocation = new Vector3 (Random.Range (-5, 5), 1f, 0f);
  GameObject clone = (GameObject)Instantiate (enemy, spawnLocation, Quaternion.identity);

}

EnemyScript:

public float MoveSpeed;

public float destroyTime;

void Start(){

  Destroy(this.gameobject);


} void Update(){

  this.tranform.Translate (Vector3.forward * MoveSpeed * Time.deltaTime);


}//this.transform means the transform of who ever is attached to this script

So now just drag your enemy prefab to the EnemySpawnScript's enemy slot.

Hope this helps...

Comment
Add comment · 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 Erin abrams · May 15, 2015 at 02:12 PM 0
Share

Thank you very much it works great, I just have one question. How can I make it so after a certain amount of time a clone will delete but just that clone that reached that time and not delete the whole prefab?

avatar image Teh_ouj · May 15, 2015 at 02:25 PM 0
Share

What do you mean? you have multiple clones and certain clones will destroy at different times? because the prefab is never destroyed. so all clones will die at the time specified.

avatar image Teh_ouj · May 15, 2015 at 02:30 PM 0
Share

Ah just put the time in the destroy statement... Destroy(Object obj, float t = 0.0F);

avatar image Erin abrams · May 15, 2015 at 02:30 PM 0
Share

I want to have it so the cubes are spawning every amount of time. But every time a cube (clone) reaches the specified time it will destroy that clone. Right now once a clone reaches 20 seconds it destroys the prefab.

avatar image Teh_ouj · May 15, 2015 at 02:33 PM 0
Share

Yes read my earlier comment please.

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.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to randomly spawn three non repeating gameobjects from an array? 2 Answers

Instatiating a prefab in a random position 0 Answers

Spawn Random Enemy 3 Answers

instantiating vertically 2 Answers

C# GameObjects Instantiate Into Each Other Issue 1 Answer


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