- Home /
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)?
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.
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
Follow this Question
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