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 Digital-Phantom · Feb 11, 2015 at 06:24 AM · instantiateprefabgui-buttonmouse position

Can't get Instantiated Object to Follow Mouse Position(Solved)

I'm trying to make it so as when I click a GUI button an object is instantiated at the mouse's position AND follows the mouse position around the screen. Unfortunately all it is doing is instantiating the Object at the mouse position at the moment I click on the GUI button and is not following the mouse around.

 using UnityEngine;
 using System.Collections;
 
 public class SatCamUpdatedGUI : MonoBehaviour
 {
     RaycastHit hit;
     private float raycastLength = 1000;
 
 
     public Texture HQBtnTex;        //Texture for the HQ Button
     public GameObject HQPrefab;        //Actual HQ prefab
     public GameObject FakeHQ;        //Mouse Pointer HQ Prefab
     
     private GameObject ChosenPrefab;
 
     void OnGUI()
     {
         if(GUI.Button(new Rect(26,26,72,72),HQBtnTex))
         {
             GameObject ChosenPrefab = FakeHQ;
             Debug.Log("HQ Chosen");
             PlaceHQPrefab();
         }
     
     
     }
 
 
     public void Update()
     {
 
     }
 
     public void PlaceHQPrefab()
     {
         //ChosenPrefab = Instantiate(Resources.Load("FakeHQ")) as GameObject;
         GameObject ChosenPrefab = Instantiate(FakeHQ, hit.point, Quaternion.identity) as GameObject;
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit, raycastLength))
         {
             Debug.Log(hit.collider.name);
             
             if(hit.collider.name == "Terrain")
             {
                 ChosenPrefab.transform.position = hit.point;
                 
             }
         }
     }
 
 }
 

I'm basing my script on an earlier script I was using that simply had an object follow the mouse continuously. -

 using UnityEngine;
 using System.Collections;
 
 public class MousePointer : MonoBehaviour
 {
 
     RaycastHit hit;
 
     private float raycastLength = 1000;
 
     void Start()
     {
     
     }
     
 
     void Update()
     {
         GameObject Target = GameObject.Find("Target");
 
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
 
         if(Physics.Raycast(ray, out hit, raycastLength))
         {
             Debug.Log(hit.collider.name);
 
             if(hit.collider.name == "Terrain")
             {
                 Target.transform.position = hit.point;
             }
         }
 
         Debug.DrawRay(ray.origin, ray.direction * raycastLength, Color.yellow);
     }
 }
 

I think (please correct me if I'm wrong) that the reason the object doesn't follow the mouse is that it isn't using the void Update() and therefor the object position isn't being updated.

However with the script I'm writing when I use the void Update() function I get objects repeatedly instantiated over and over until the screen is full of them...lol

How do I change it so I get just a single object Instantiated AND that object follows my mouse?

Any suggestions please guys ???

Comment
Add comment · Show 1
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 Digital-Phantom · Feb 11, 2015 at 07:30 AM 0
Share

O$$anonymous$$, not really sure if this is a step in the right direction(or not) but its different. Have adjusted the script to this -

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ouseInstantiateAndFollow : $$anonymous$$onoBehaviour
 {
     RaycastHit hit;
     private float raycastLength = 1000;
     
     
     public Texture HQBtnTex;        //Texture for the HQ Button
     public GameObject HQPrefab;        //Actual HQ prefab
     public GameObject FakeHQ;        //$$anonymous$$ouse Pointer HQ Prefab
     
     private GameObject ChosenPrefab;
     
     void OnGUI() //Places the Object in the World
     {
         if(GUI.Button(new Rect(26,26,72,72),HQBtnTex))
         {
             GameObject ChosenPrefab = Instantiate(FakeHQ, hit.point, Quaternion.identity) as GameObject;
             Debug.Log("HQ Chosen");
         }
         
         
     }
 
     void Update() //Allows Object to Follow $$anonymous$$ouse like in Original Script
     {
         GameObject Target = ChosenPrefab;
         //GameObject Target = GameObject.Find("ChosenPrefab");
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit, raycastLength))
         {
             Debug.Log(hit.collider.name);
             
             if(hit.collider.name == "Terrain")
             {
                 Target.transform.position = hit.point;
             }
         }
         
         Debug.DrawRay(ray.origin, ray.direction * raycastLength, Color.yellow);
     }
 
 
 }


But I'm now getting a compile error -

NullReferenceException: Object reference not set to an instance of an object $$anonymous$$ouseInstantiateAndFollow.Update () (at Assets/Scripts/$$anonymous$$ouseInstantiateAndFollow.cs:40)

Am I right in thinking that line 40 is throwing that error because the Update function is immediately looking for the game object? And obviously the object will not be there until the player clicks the GUI button ?

If so... how do I make it wait until after the player has clicked the button ?

???

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by hexagonius · Feb 11, 2015 at 07:45 AM

I recommend you separating the scripts. One script for the Gui, the update on the prefab directly. Right now update is running without testing chosenprefab for null, which it is until you press the gui button.

Comment
Add comment · 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 Digital-Phantom · Feb 11, 2015 at 09:46 AM 0
Share

O$$anonymous$$, I tried separating the scripts.

If I place the object into the scene and then play it happily follows the mouse position wherever I point (using this script)

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ousePointerUpdated : $$anonymous$$onoBehaviour
 {
 
     public GameObject Target;

     RaycastHit hit;
     
     private float raycastLength = 1000;
     
     void Start()
     {
         
     }
     
     
     void Update()
     {
         GameObject Target = GameObject.Find("HQFake");
         
         Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
         
         if(Physics.Raycast(ray, out hit, raycastLength))
         {
             Debug.Log(hit.collider.name);
             
             if(hit.collider.name == "Terrain")
             {
                 Target.transform.position = hit.point;
                 PlaceHQPrefab 
             }
         }
         
         Debug.DrawRay(ray.origin, ray.direction * raycastLength, Color.yellow);
     }
 }
 
 

If I instantiate the object into the scene using the GUI button (this script)

 using UnityEngine;
 using System.Collections;
 
 public class $$anonymous$$ouseInstantiateAndFollow : $$anonymous$$onoBehaviour
 {
     RaycastHit hit;
     private float raycastLength = 1000;
     
     
     public Texture HQBtnTex;        //Texture for the HQ Button
     public GameObject HQPrefab;        //Actual HQ prefab
     public GameObject FakeHQ;        //$$anonymous$$ouse Pointer HQ Prefab
     
     public GameObject ChosenPrefab;
     
     void OnGUI()
     {
         if(GUI.Button(new Rect(26,26,72,72),HQBtnTex))
         {
             Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
             
             if(Physics.Raycast(ray, out hit, raycastLength))
             {
                 
                 if(hit.collider.name == "Terrain")
                 {
                     GameObject ChosenPrefab = Instantiate(FakeHQ, hit.point, Quaternion.identity) as GameObject;
                 }
                     
             }
         }
         
         
     }
 
 }
 
 

The object just sits there and does not move. Then when/if I move the mouse the game crashes and I get this error -

NullReferenceException: Object reference not set to an instance of an object $$anonymous$$ousePointerUpdated.Update () (at Assets/Scripts/$$anonymous$$ousePointerUpdated.cs:29)

???

avatar image hexagonius · Feb 11, 2015 at 10:10 AM 1
Share

Remove the Target Reference from the $$anonymous$$ousePointerUpdated script. Since you ARE that GameObject, just manipulate transform.position. That's it. So line 21 is obsolete, remove Target from line 31 and just use transform.position.

P.S. In the $$anonymous$$ouseInstantiateAndFollow Script you don't need to save the instantiate to ChosenPrefab, since you're not doing anything with it. Just remove the assignment (this is optional)

avatar image Digital-Phantom · Feb 11, 2015 at 11:55 AM 0
Share

Thanks, that sorted it.

avatar image
1
Wiki

Answer by lordlycastle · Feb 11, 2015 at 08:45 AM

The reason for player getting instantiated repeatedly when you call the PlaceHQPrefab function from the update is because, you didn't put it inside an if condition; that checks if the mouse was clicked or not. So instead of calling PlaceHQPrefab directly from OnGUI you could use a bool variable to indicate the button was clicked.

     bool buttonClicked = false;
     float speed = 5.0f;
     GameObject ChosenPrefab;
 
     public void OnGUI(){
         if (GUI.Button(new Rect(26,26,72,72),HQBtnTex)){
             //Do some stuff to the button like change colour when clicked, or display player count.
             buttonClicked = true;
         }
     }
 
     void Update(){
         if (Input.GetMouseButtonDown(0) && buttonClicked){
             print("Left clicked!");
             PlaceHQPrefab();
             buttonClicked = false;
         }
 
         if (ChosenPrefab != null){
             //Temp code to make the player follow the current mouse position
             ChosenPrefab.transform.position = Vector3.Lerp(ChosenPrefab.transform.position, Input.mousePosition, speed * Time.deltaTime);
         }
      }

Obviously you shouldn't use Lerp function to move the player, it might be jittery. Use the move function on the character controller.

P.S. in your comment, the error is probably caused because you didn't instantiate the game object and tried to change it's transform position, you can easily avoid that by using

 if(gameObject != null){
     print(gameObject.name + "does exist, and it's position is:" + gameObject.transform.position);
 }
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.

Follow this Question

Answers Answers and Comments

21 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

Related Questions

How to Add Y Axis Offset to transform.localPosition on an Instantiated Prefab? 2 Answers

Referencing gameObject from script after Instantiate 0 Answers

Instantiating one prefab with different materials in one frame 0 Answers

Prefab of a whole scene? 3 Answers

how to destroy camera instatiated from prefab? 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