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 austen 2 · Mar 28, 2011 at 03:57 PM · javascriptguitransformmouse

instantiate button problem

Hello, what i am trying to do is when i click my GUI button, i want a prefab to be instantiated, then i want to transform it to follow my mouse. Here is my script

var cube2 : GameObject; var cubetex : Texture;

function OnGUI () {

       var screenPos = Camera.main.WorldToScreenPoint(transform.position); 
       var offset = transform.position -                                                Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z));

   var curScreenPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z); 
       var curPos = Camera.main.ScreenToWorldPoint(curScreenPos) + offset; 



 if (GUI.Button(Rect (20,20,60,60), cubetex)) {

     Instantiate(cube2, Vector3(0,0,0), Quaternion.identity);
             cube2.transform.position = curPos;
 }

}

I know that my variables for curPos are right because If i use it on another script i am able to drag objects with it. what i need to know Is that how you transform the object?? i cant get it to move from vector(0,0,0), even when i change (cube2.transform.position = curPos;) to (cube2.transform.position = Vector(14,3,11)), it still stays at 0,0,0. Please help

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by e-bonneville · Mar 28, 2011 at 04:27 PM

There are several problems with your script. First, you aren't using Instantiate correctly (see my code below for a correct implementation). Secondly, even if your code worked, you'd only be moving your cube to the mouse position for the one frame in which you click the button. The code below should do the trick; please note, however, that it is untested.

var cube2 : Transform; var curCube : Transform;

var cubetex : Texture;

function Update() { if (curCube != null) { curCube.transform.position = curPos; } }

function OnGUI () {

 var screenPos = Camera.main.WorldToScreenPoint(transform.position); 
 var offset = transform.position - Camera.main.ScreenToWorldPoint(Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z));

 var curScreenPos = Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPos.z); 
 var curPos = Camera.main.ScreenToWorldPoint(curScreenPos) + offset; 

 if (GUI.Button(Rect (20,20,60,60), cubetex)) {
     curCube = Instantiate(cube2, Vector3(0,0,0), Quaternion.identity);
 }

}

Untested JS, please report any errors

Comment
Add comment · Show 1 · 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 austen 2 · Mar 28, 2011 at 05:04 PM 0
Share

i get the error: InvalidCastException: Cannot cast from source type to destination type. button.OnGUI () (at Assets/SCripts/button.js:32)

on line: curCube = Instantiate(cube2, Vector3(0,0,0), Quaternion.identity);

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

No one has followed this question yet.

Related Questions

How can I display text and change color of objects when I roll over them with the mouse? 1 Answer

Check if mouse is over a button 1 Answer

Setting Scroll View Width GUILayout 1 Answer

trying to get an object to follow mouse on GUI click 1 Answer

how to set input mouse position, to a rect of a gui button that also needs to follow a different rect 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