• 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 everettdale · Apr 17, 2014 at 07:19 PM · move speed

How do I move a prefab(clone) in a non attached script

I'm trying to Instantiate a clone in a master control script and get it to move. I can't figure out how to affect the instantiated clone.

using UnityEngine; using System.Collections;

public class MasterScript : MonoBehaviour {

 public Rigidbody2D cat;
 public float speed = 10f;

 // Use this for initialization
 void Start () {
 
     cat = (Rigidbody2D)Instantiate (cat, transform.position, transform.rotation);
     cat.transform.Translate(Vector2(0,speed));

 }

}

I'm sure this is easy it's just beyond me for some reason.

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Burla · Apr 17, 2014 at 09:06 PM

Are there any particular reason to why you're casting it to/as a Rigidbody2D?

A Rigidbody doesn't have a transform. You should make it a GameObject instead as it has a transform.

 public GameObject cat;

 cat = Instantiate(cat, transform.position, transform.rotation);
 cat.transform.Translate(Vector2(0, speed));

Try doing it this way. Please let me know if this solved your problem. Please feel free to correct me if I'm wrong on this.

Comment
Add comment · Show 4 · 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 robertbu · Apr 17, 2014 at 09:09 PM 1
Share

@Burla - a Rigidbody does have a transform. Or put another way, a Rigidbody is a component and therefore cannot exist without a game object. Therefore the game object the Rigidbody is attached to will have a transform. And you can access that Transform through the rigidbody.

avatar image everettdale · Apr 17, 2014 at 10:09 PM 0
Share

Still not working. I may have not given enough information.

First no reason for the Rigidbody2D except that was what I thought I needed.

Second This script is attached to an empty game object, no the prefab or the clone. (if that matters)

Third All I want to do it Instantiate an object then control it as simply as possible.

However the above suggestion does not work either. Here is the new code and the errors

 using UnityEngine;
 using System.Collections;
 
 public class MasterScript : MonoBehaviour {
 
     public GameObject cat;
     public float speed = 10f;
 
     // Use this for initialization
     void Start () {
     
         cat = Instantiate(cat, transform.position, transform.rotation);
         cat.transform.Translate(Vector2(0, speed));
 
     }
 
 }

I get these errors

Assets/Scripts/MasterScript.cs(12,17): error CS0266: Cannot implicitly convert type UnityEngine.Object' to UnityEngine.GameObject'. An explicit conversion exists (are you missing a cast?)

Assets/Scripts/MasterScript.cs(13,41): error CS0119: Expression denotes a type', where a variable', value' or method group' was expected

Assets/Scripts/MasterScript.cs(13,31): error CS1502: The best overloaded method match for UnityEngine.Transform.Translate(UnityEngine.Vector3)' has some invalid arguments Assets/Scripts/MasterScript.cs(13,31): error CS1503: Argument #1' cannot convert object' expression to type UnityEngine.Vector3'

avatar image robertbu · Apr 17, 2014 at 10:11 PM 0
Share

Change lines 12 and 13 as follows to get rid of the errors:

 cat = Instantiate(cat, transform.position, transform.rotation) as GameObject;
 cat.transform.Translate(new Vector2(0, speed));

Note the translate is only going to cause the Instantiated object to move once. If you have a rigidbody, you may want to use cat.rigidbody.AddForce() to get it to move and continue to move.

avatar image Burla · Apr 18, 2014 at 10:23 AM 0
Share

@robertbu - Ah, okay. I didn't know that you can access the transform through the rigidbody. Does that mean that you can do it the other way too - access the rigidbody through a transform as they're both components of a GameObject?

avatar image
0

Answer by everettdale · Apr 19, 2014 at 07:43 AM

Ok. I made these changes to lines 12 and 13

cat = Instantiate(cat, transform.position, transform.rotation) as GameObject; cat.transform.Translate(new Vector2(0, speed));

The object shows up as cat(clone) but does nothing. I get this error while the game is running

NullReferenceException: Object reference not set to an instance of an object MasterScript.Start () (at Assets/Scripts/MasterScript.cs:13)

I don't understand why this is so difficult. It seems like a basic idea. Create an object in the scene and make it move, from point a to point b or whatever. I'd think this would be a common thing to do in most games.

Comment
Add comment · 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
0

Answer by SkyforceP · Feb 05, 2016 at 04:41 PM

Hi, i have the same problem, my main object moves but its clones not. I do it in this way

1)Generating my cubes on the scene

 public class random_generating : MonoBehaviour {
 
     public GameObject Cube;
 
     // Use this for initialization
     void Start () {
         for(int i=0; i<5; i++)
         Instantiate (Cube,new Vector3(1.5f,3.34f,0.0f),Quaternion.identity);    
     }
     
 }

2)Cube moving

 public class cube_moving : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         transform.Translate (0, 0, Time.deltaTime*(-5));
         //transform.Translate(0, Time.deltaTime, 0, Local.World);
     }
 }

I know that my piece of advice does not allow your problem, but as another idea can little inspire you for smth else(sorry for my english, i am ukranian))

Comment
Add comment · 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

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

22 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

Related Questions

How can I randomize the speed of enemy movement? 2 Answers

Rididbody movement at the end of a rotating platform 0 Answers

Collide with a 'switch' gameobject that lowers the speed of another gameobject 1 Answer

Move to a position on buttonhold? 1 Answer

C# trying to move a object 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges