Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Flaose · May 20, 2017 at 08:49 AM · c#scripting problemgameobjecttransform

Transforming a GameObject with an unattached script

I'm new to Unity user and this is my first post so I apologize if this has been asked a million times (and I just wasn't clever enough to find the answer in my searches) or I break some other rule of etiquette.

I went through the Breakout Game for Beginners Tutorial and wanted to tweak the finished product so that when the ball collides with the top of the arena its speed greatly increases and the paddle gets significantly shorter. While I eventually figured out a satisfactory way to increase the ball speed when colliding with the top, I had a very hard time efficiently transforming the "Paddle" GameObject.

In Ball.cs (which is attached to the "Ball" GameObject) I declared the Boolean hitTop and added the following collision code:

         void OnCollisionEnter(Collision col)
         {
             if(col.gameObject.name == "Roof" && hitTop == false)
             {
                 hitTop = true;
                 Vector3 dir = col.contacts[0].point - transform.position;
                 dir = -dir.normalized;
                 GetComponent<Rigidbody> ().AddForce (dir * speedUp);
 //              This is where I want to add code that will transform the GameObject named "Paddle"         
             }
         }

I would like to replace the comment line with C# code that will allow me to reference the GameObject named "Paddle" and perform the following transformation on it:

             transform.localScale -= new Vector3 (1.5f, 0, 0);

I was eventually able to figure out a sloppy workaround by checking static variables in the Update() function of Paddle.cs but is there a simpler/more correct/more elegant way to change the transform of a GameObject using a script that's attached to an unrelated GameObject (like Ball.cs)?

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

Answer by Vandarthul · May 20, 2017 at 09:47 AM

First of all, welcome to the community and Unity. Now I'll try to explain this as much as I can. There are lots of ways to do that, but I think what you are asking is not about the situation you are experiencing but you would like to know the proper way. But first let me offer you a simple solution to your problem: Assign a reference to Ball.cs of Paddle.cs and modify it when you need it:

  public Paddle paddle; // Assign this public reference on inspector
 void OnCollisionEnter(Collision col)
          {
              if(col.gameObject.name == "Roof" && hitTop == false)
              {
                  hitTop = true;
                  Vector3 dir = col.contacts[0].point - transform.position;
                  dir = -dir.normalized;
                  GetComponent<Rigidbody> ().AddForce (dir * speedUp);
                  paddle.transform.localScale -= new Vector3(1.5f, 0, 0);
              }
          }

But as you've guessed this is kind of wrong. Ball should not know what Paddle is. Yet it shouldn't even modify Paddle in any way. So a better way to do this is to have a Manager object which holds reference to the Paddle and Ball and control the game logic. This is just fine for small projects like this one, but if the project gets bigger, this will be a problem too. If all your logic is handled in your GameManager, there is a big chance of breaking other thing when you change something that is related to GameManager.

A better way of doing this is perhaps with events. I don't know your knowledge of programming but if you just started, I recommend you not to look at these just yet. Try to make games and understand how things work, and think about how they should work, and constantly challenge your mind to how you can do the same thing in a better way.

Here is a link if you would like to learn about events. If you don't understand what's going on, as I've said earlier try to continue with learning basic/intermediate stuff until you feel the need of advanced stuff like this. Also, Check this link if you like to learn what you will need to learn in the future.

Good luck with your studies.

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 Flaose · May 20, 2017 at 06:30 PM 0
Share

This is exactly what I needed, thanks!

The links will also be quite useful going forward, though I'm a program$$anonymous$$g rookie so I still have a lot to learn before I get my around around events.

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

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

How to keep a Gameobject in the same position after a transform.Rotate? 2 Answers

Cannot destroy Component while GameObject is being activated or deactivated 2 Answers

Adding script during runtime 2 Answers

Shear transformation using GameObject transformations 2 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