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 Kavman · Mar 09, 2014 at 12:00 PM · gameobjecttransform.positiongrid

Transform does not move GameObject

Hi, I'm pretty new to c# and unity so please bare with me. I want to transform.position a Gameobject on a grid by using collider.bounds.center as a vector 3 reference. I am trying to do this through a tilescript which equals one tile in an array. When I instantiate the GameObject/PlayerPrefab I am able to clone the Gameobject onto each tile (which is what I want to do with the transform.position). However when I try to transform.position the GameObject nothing works.

 public class TileScript : MonoBehaviour {
     
     public Vector3 tileCenter;
     public GameObject PlayerPrefab;
     public PlayerScript Select;
     public float x,y,z;
 
     // Use this for initialization
     void Start () {
         //tileCenter = new Vector3 (x, 3, z);
 
         x = this.transform.collider.bounds.center.x;
         y = this.transform.collider.bounds.center.y;
         z = this.transform.collider.bounds.center.z;
 
         }        
     void Update () {
         
     }
 
     
     void OnMouseEnter() {
         gameObject.transform.renderer.material.color = Color.blue;
     }
 
     
     void OnMouseExit() {
         transform.renderer.material.color = Color.white;
         }
 
     public void OnMouseDown()
         {
         PlayerPrefab.transform.position =  new Vector3 (x, 3, z);
         //if (Select.selected == true) 
             //var Move = Instantiate(PlayerPrefab, new Vector3 (x,0,z), Quaternion.AngleAxis (0,Vector3.right));        
             //tileCenter = newPos;
             //print (x);
     }
 }
         



I have tried multiple variations of transform.position, none seem to work. Any help would be appreciated, thanks.

Comment
Add comment · Show 5
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 · Mar 09, 2014 at 12:01 PM 1
Share

If player prefab is a prefab you would need to instantiate it first.

avatar image Kavman · Mar 09, 2014 at 02:29 PM 0
Share

Thankyou for your response. You mean having a prefab already dragged out onto the scene does not count as a Gameobject that I can move?

avatar image kestik · Mar 09, 2014 at 03:06 PM 0
Share

You can set tranform's position after its instantiation, but not before.

public void On$$anonymous$$ouseDown() { if (Select.selected == true) var $$anonymous$$ove = Instantiate(PlayerPrefab, new Vector3 (x,0,z), Quaternion.AngleAxis (0,Vector3.right)); $$anonymous$$ove.transform.position = new Vector3 (x, 3, z); // moving tileCenter = newPos; print (x); }

avatar image robertbu · Mar 09, 2014 at 03:17 PM 0
Share

"You mean having a prefab already dragged out onto the scene does not count as a Gameobject that I can move?"

The confusion is about prefabs. A true prefab does not exist in the scene. It is a blueprint for an object.

https://docs.unity3d.com/Documentation/$$anonymous$$anual/Prefabs.html

As for your issue, your code should work if you are clicking on the object and there are no other colliders between the camera and the object. On$$anonymous$$ouseDown() only fires if you click on the specific object that has that callback.

avatar image Kavman · Mar 09, 2014 at 03:32 PM 0
Share

Thanks for all the feedback. I think leaving in the commented code has added to the confusion. What I'm trying to say is that I want Transform.position to do what Instantiate is doing but moving the object ins$$anonymous$$d of cloning it like what transform.position is supposed to do. Your explanation has kind of made me understand why instantiate works and transform.position does not. Thanks I'll keep banging away at it and let you know how I go.

1 Reply

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

Answer by Kavman · Mar 09, 2014 at 05:39 PM

Ok I got it to work by instantiating the prefab on the grid creation script and transform.position in the player script on Input.mouseButtonDown (0)). Using Raycast though but atleast it works. Thanks for the input.

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

GUI Buttons and game objects? 1 Answer

Moving a GameObject to a certain point in world space via script 1 Answer

Instantiate, problem on transform position 1 Answer

How do I create roadblocks in my Match 3 puzzle gameboard? 2 Answers

Help with C# - destroy at a certain position not working 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