Jump to content
  • Advertisement
ethancodes

C# Error for missing Rigidbody2D

Recommended Posts

Hopefully this is an easy fix, but I'm getting an error for trying to access the Rigidbody2D on my LevelManager gameobject. Now, I would understand this no problem, except I'm not trying to access my LevelManager anywhere. At least not intentionally. I found the line of code that is causing the error is here:

void Awake ()
	{
		this.GetComponent<Rigidbody2D> ().velocity = new Vector2 (2f, 10f);
	}

This is a method on my BasePickUp abstract class. This class has several child classes that are implemented on a few pick ups. I tried changing 'this' to 'gameObject', but got the same results. And the code is doing what I want. If I take it out, the pickups just fall slowly down the screen, put the code back in and the pickups shoot off in a certain direction just like I want. So it is working, but it's also causing this weird error. Any ideas?

Share this post


Link to post
Share on other sites
Advertisement
3 hours ago, ethancodes said:

So it is working, but it's also causing this weird error. Any ideas?

As the error says your manager doesn't have a rigidbody. However I assume you didn't want it to have one as it is common for managers to be static.

If you do want it to move then just add a rigidbody component to it.

If you want the manager to be stationary but the children to move then call that line on the children like this:

this.transform.transform.GetChild(0).GetComponent<Rigidbody2D> ().velocity = new Vector2 (2f, 10f);

The GetChild(Index) finds the child with the right index. For example 0 = first child object. So often you would use a loop to set a property to each child; instead of setting it one by one.

Like this:

int Index = 0;

while (Index < this.transform.childCount){
  this.transform.transform.GetChild(Index).GetComponent<Rigidbody2D> ().velocity = new Vector2 (2f, 10f);
  Index +=1; //Remember to add a number to the loop or it will freez Unity as it never ends.
}

So the above code will on awake check that each child is set to a velocity of (2,10).

Share this post


Link to post
Share on other sites

I guess I'm confused though as to why it's moving the LevelManager. The script isn't attached to the level manager and I'm not calling it by name or anything anywhere. 

Share this post


Link to post
Share on other sites
5 minutes ago, ethancodes said:

The script isn't attached to the level manager and I'm not calling it by name or anything anywhere. 

What is the script attached to?

Share this post


Link to post
Share on other sites

It's attached to the BasePickUp class, which is an abstract class. The sub classes of it are the scripts for each of my pick ups.

 

Share this post


Link to post
Share on other sites
Posted (edited)
29 minutes ago, ethancodes said:

It's attached to the BasePickUp class, which is an abstract class.

So is the manager also a inheritance of the BasePickUp class or in any way related to it?

Or Maybe you adjusted/ extended the Mono class?

Edited by Scouting Ninja

Share this post


Link to post
Share on other sites

No neither one. It's not even like the LevelManager even spawns the pickups because currently I'm just dropping the prefabs into the hierarchy. One thing that just came to mind, and this is a bit of a long shot but I'll check it when I get home, but I suppose it is possible I dropped the pick ups onto the LevelManager in the hierarchy by accident, making them children of it. That would explain why I didn't get this error before. Could just be a stupid mistake. I'll check tonight. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!