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);
}
}