Unity/JavaScript Scripting Question from Tutorial

Updated on September 29, 2017 in [A] Tutorials
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on Reddit0
2 on September 21, 2017

Hey everyone, 

 

I am brand new to the Brackeys community so if this is the wrong category for this I would like to apologize in advance. I recently started watching and following along in the Make a Game series. I am using Microsoft Visual Studio 2017 and Unity 5.1 instead of Xamarin/Monodevelop and Unity 4.2. I came across something that made me curious because while my goal is to develop games, I also want to learn about programming languages and their uses as much as possible.

 

So in the second video, I believe, Brackey begins showing us how to cause a ball to rotate using scripting in JavaScript. The line he uses is as follows:

 

rigidbody.AddRelativeTorque (Vector3.back * rotation);

 

Now I feel like with my lack of experience and the explanation that brackey gives I have a decent concept of what is going on here, however, once I completed my script, including this line. Unity asked to change my script to

 

GetComponent.().AddRlativeTorque (Vector3.back * rotation);

 

I understand that this is basically just a syntax change, but I was curious about as to why it happened and if there is any actual difference in the script lines. If possible could someone also briefly explain what kind of impact this will have as I continue to program and what things to look out for in scripting for Unity? Obviously this is a broad request so I understand if it is difficult or impossible to predict what kind of issues I may run into. I apologize if this seems like a stupid question but I have found that the more that I know about a System, regardless of whether that is a System like the connection between Unity and JavaScript, or if it is the Hydrogen Infrastructure that I work on daily, I am able to much more readily understand the System as I gain knowledge regarding it. 

 

Thank you and I apologize for the long windedness of my question/comments.

  • Liked by
Reply
1 on September 22, 2017

So this tutorial is really old. I watched it a couple times a few years ago, so my memory might be a bit foggy, but I’ll try and answer this one as best I can.
 
This tutorial was created back in the days of early Unity 4.x releases, I believe… Back then you could just say rigidbody and it automatically referred to an attached Rigidbody component (the same way you can still just say ‘transform’ to refer to the Transform component).
Since the recent updates though (to Unity 5.x I believe, they don’t allow that for rigidbody anymore. Now, you have to explicitly use GetComponent(Rigidbody) (or GetComponent<RigidBody>() which is the one I use) when you want to use it.
So here’s what you can add to your script instead to make it work:

Rigidbody rigidbody = GetComponent<Rigidbody>(); //put this as a public variable at the top of your class as a reference

This way, now you can pretty much use the same scripts he does (at least concerning Rigidbody… you’ll probably have to do a few of these for other components too).

on September 29, 2017

Thank you Beal, (and I apologize for my delayed response, my normal job got in the way of my scripting). I appreciate your explanation and the time you took to make it. As I get farther into JavaScript and into these tutorials I am understanding more. Even though these tutorials are for an older version of Unity, I have continued to do them as a bit of an added challenge. I knew that not everything would be the same as what Brackey was doing and hoped that in finding certain answers and solutions to the issues that came up from the differences in Scripting and Unity I would learn a little bit better how to use Unity and how to Script it with JS. Even now I’m having to fight with this GetComponent<>() and AudioSource. So I’m hoping to improve my learning by forcing myself to do some of it on my own.  

Show more replies
  • Liked by
Reply
Cancel