Following round planet tutorial, line of code not working?

Hello, I’m following a tutorial on how to make a round planet with gravity, but it seems that the “Attract” part of the line of code is turning red in MonoDevelop for me, and is giving an error.

[Unity Tutorial] First Person Controller: Spherical Worlds - YouTube at 7:53

Error that I am getting:
Assets/Scripts/GravityBodyScript.cs(20,24): error CS1061: Type GravityBodyScript' does not contain a definition for Attract’ and no extension method Attract' of type GravityBodyScript’ could be found. Are you missing an assembly reference?

edit(moved from answer)

This is the code he has right before he runs the game.

The script below is used for the planet.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GravityAttractorScript : MonoBehaviour {

	public float Gravity = -10;

	public void Attract(Transform Body){
		Vector3 TargetDirection = (Body.position - transform.position).normalized;
		Vector3 BodyUp = Body.up;

		Body.rotation = Quaternion.FromToRotation (BodyUp, TargetDirection) * Body.rotation;
		Body.GetComponent<Rigidbody>().AddForce (TargetDirection * Gravity);
	}
}

The script below is used for the object on the planet.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

[RequireComponent (typeof (Rigidbody))]
public class GravityBodyScript : MonoBehaviour {

	GravityBodyScript VarGravityBodyScript;
	Rigidbody RigidBody;

	void Awake () {
		VarGravityBodyScript = GameObject.FindGameObjectWithTag ("Planet").GetComponent<GravityBodyScript> ();
		RigidBody = GetComponent<Rigidbody>();

		RigidBody.useGravity = false;
		RigidBody.constraints = RigidbodyConstraints.FreezeRotation;
	}

	void FixedUpdate(){
		VarGravityBodyScript.Attract (RigidBody);
	}
}

There are different things going on:

The method ‘Attract’ takes a Transform as a parameter, but in your other script you are calling the method and pass a Rigidbody. This should not compile, unless you have another Attract method that accepts the Rigidbody parameter.

Secondly, you are calling GameObject.FindGameObjectWithTag ("Planet").GetComponent<GravityBodyScript> (); and later VarGravityBodyScript.Attract (RigidBody);, but this doesn’t make sense, because you defined the ‘Attract’ method in the ‘GravityAttractorScript’.

Both of these issues need fixing. So probably, you want to call GetComponent<GravityAttractorScript>() and also change the Transform parameter to a Rigidbody. :wink:

Also you can rewatch the tutorial one more time and compare your code, it looks like it’s correct in the video, but you’ve mixed up the Body and Attractor scripts. Happens ^^

You are passing a Rigidbody object to a function that takes a Transform. In the video he passes “transform” which is the object’s own transform data.