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 Jonathan07 · Apr 16, 2014 at 02:07 AM · gameobjectrigidbodymove

Moving a GameObject with a script from another GameObject (Rigidbody)

Hi guys, I'm trying to have a GameObject move using it's rigidbody from a script. I seem to be unable to do it from another GameObject. This script is attached to "_GameController":

 using UnityEngine;
 using System.Collections;
 
 [System.Serializable]
 public class Boundary{
     public float xMin, xMax, yMin, yMax;
 }
 
 public class PlayerMovement : MonoBehaviour {
 
     public Boundary boundary;
     public float tilt;
     public float speed;
     public Rigidbody player;
 
     void Start(){
         player = GameObject.Find("ship").GetComponent<Rigidbody>();
         boundary.xMax = 8.5f;
         boundary.xMin = -8.5f;
         boundary.yMax = 4.5f;
         boundary.yMin = -4.5f;
     }
 
     void FixedUpdate(){
         float moveHorizontal = Input.GetAxis("Horizontal");
         float moveVertical = Input.GetAxis("Vertical");
         Vector3 movement = new Vector3(moveHorizontal, moveVertical, 0.0f);
         player.velocity = movement * speed;
         player.position = new Vector3
             (Mathf.Clamp(rigidbody.position.x, boundary.xMin, boundary.xMax), Mathf.Clamp(rigidbody.position.y, boundary.yMin, boundary.yMax), 0);
         player.rotation = Quaternion.Euler (0.0f, 0.0f, rigidbody.velocity.x * -tilt);
     }
 }



This script is meant to make "player" move. I know it's setting the variable right but it's telling me that "_GameController" has no rigidbody even though I'm not even trying to access it. Any ideas?

Now I know you will tell me to just put it on the Object i desire to move but the thing is, the object is a ship and the player can buy many ships with different speeds and everything so it would require a lot more work.

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
1
Best Answer

Answer by robertbu · Apr 16, 2014 at 03:45 AM

The problem is that on line 30, you are making references to 'rigidbody'. That will be on the same game object as _GameController script is attached to. I believe what you really want for these references is 'player'. That is, line 29/30 should be:

 player.position = new Vector3 (Mathf.Clamp(player.position.x, boundary.xMin, 
     boundary.xMax), Mathf.Clamp(player.position.y, boundary.yMin, boundary.yMax), 0); 
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 Jonathan07 · Apr 16, 2014 at 11:15 AM 0
Share

Great! I don't get the error anymore! Thanks! Although the object still doesn't move but that's probably a whole other problem. Thanks for the help!

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

20 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

Related Questions

Moving rigidbodies at random 1 Answer

Aircraft controller script 2 Answers

add rigidbody to item and it acts weird? 1 Answer

Problem dragging, scaling, moving objects in #scene view with mouse 0 Answers

How can I access other scripts and their functions? 3 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