Error for missing Rigidbody2D

Started by
6 comments, last by ethancodes 5 years, 10 months ago

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?

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).

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. 

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?

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.

 

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?

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. 

This topic is closed to new replies.

Advertisement