• 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 programming 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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

Add transforms to Transform[] list at runtime 1 Answer

How do i change a sprite when another gameobject with the same prefab is colliding / is near 1 Answer

this.transform.FindChild(x) and GameObject.Find(x) returning (apparently?) different results 1 Answer

MoveTowards issue, animating ui. 1 Answer

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

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges