• 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
0
Question by kerperlo · Mar 29, 2016 at 03:21 PM · c#instantiatemousefollowclone

Can't Get Prefab Clone To Follow Mouse

I already looked up questions for the following question, however I am still not getting the results I need. I created the Prefab called "Ground". I then made a script called "PrefabCreation".

Prefab Creation:

 using UnityEngine;
 using System.Collections;
 
 public class AssetCreation : MonoBehaviour {
 
     public GameObject target;
 
     Vector3 mousePos;
 
     void Start(){
     }
 
     void Update(){
         mousePos = Input.mousePosition;
         mousePos.z = 10f;
 
         target.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
     }
 }

In order for the Prefab to follow the mouse I need to drag the selected Prefab into the scene, then use the created Prefab's transform as the reference. It works, but not what I needed.

When creating an instance/clone of the Prefab into the game during run time when a key is pressed, I use this code:

 Transform groundT = (Transform)Instantiate (target, transform.position, transform.rotation);

However, I can't get that current clone to follow the mouse. The code above is in an Update function. I am using C#.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by malta32 · Mar 30, 2016 at 05:30 AM

Try assigning the Instantiated game object to a variable.

  using UnityEngine;
  using System.Collections;
  
  public class AssetCreation : MonoBehaviour
 {
 
     public GameObject preFab;
 
     public GameObject target;
 
     Vector3 mousePos;
  
     void Start()
     {
         target = (GameObject)Instantiate(preFab);
     }
 
     void Update()
     {
         mousePos = Input.mousePosition;
         mousePos.z = 10f;
 
         target.transform.position = Camera.main.ScreenToWorldPoint(mousePos);
     }
 }








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 kerperlo · Mar 31, 2016 at 07:46 PM

Thank you @malta32! That worked out very well!

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

I need help about Basic Basketball Game 0 Answers

Random.range game object destroying itself before reaching destroy position.Please help. 1 Answer

Edit variable in instantiated clone 1 Answer

How can I change this script so that each instantiated prefab spawns a set amount faster 0 Answers

Instantiation of my GameObjects spell (from other script) 0 Answers

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