Outdated API Error

Following the lecture, I get a compiler error when following the step to make the lazer move

beam.rigidbody2D.velocity = new Vector3 (0, projectileSpeed, 0);

the error dialogue says

UnityEngine.GameObject.rigidbody2D' is obsolete:Property rigidbody2D has been deprecated. Use GetComponent() instead. (UnityUpgradable)’

Trying to solve myself but no real idea how that updated call effects the syntax as a whole

If any other script noobs hit same problem as me… make a back up and run the api updater

Assets > Run API Updater

It will just swap out that line so you could do it manually but basically instead of this shown in video…

beam.rigidbody2D.velocity = new Vector3 (0, projectileSpeed, 0);

it appears you actually need to say…

beam.GetComponent().velocity = new Vector3 (0, projectileSpeed, 0);

My worry when I see an error like this is I dont really know how it effects the whole piece of code and whether its as easy as wapping out one or two words of if the whole thing needs refactoring. Looking like in this case its a really simple swap.

Scrap my previous reply… whilst that update stopped the compiler error, the laser beam goes no where so a bit blocked here by the obsolete code. If anyone knows how to modify the expression to work with the new API would be really grateful for a steer

Not sure if you already found the answer, but I was stuck as well. Here is how I have done it so far and it appears to be working.

Of note I’m using Unity 5.6.1. Don’t worry about the change I made in the KeyCode.Space by making it GetKeyDown instead of GetKey ( I was experimenting). Anyways, in Unity 5 we have to extract Rigidbody2D from the GetComponent since the way in the video is deprecated. Afterwards, just tell it which direction it should go. The next line contains the actual movement for the beam and speed (projectileSpeed). Hope this helps! BTW I’m still a beginner so don’t take this code to heart, but it’s worked for me thus far.

Just found out you can delete the “beam.transform.position” line. You’re all set with the rest of the code, once you add the rigidbody2d to the beam in the editor, under “Add Component”.

Haha the only thing holding it up after my update API edit was entering a value for the speed I forgot the float was exposed on the player for the laser… so wasn’t looking on there. So in conclusion anyone with same problem just needs to run the Ali update and make sure they have something other than zero set for blaster speed and you’re golden

Cheers for replying Mike

Privacy & Terms