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.
https://www.youtube.com/watch?v=TicipSVT-T8 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);
}
}
Did you write the Attract method in the GravityBodyScript? Is the method public? How did you get a reference to the GravityBodyScript? There are so many open questions about what you already have. $$anonymous$$aybe we also need to see some code and more info about what you've implemented. The error message looks like the Attract method just doesn't exist yet or you are referencing the wrong type (class/script).
Yes to all of those things, my code basically looks like his code.
It's possible that he adds additional code or does some setup later, but the error message is clear, that it can't find the method. Either it doesn't exist (spelling), or it doesn't exist on the referenced object (is the variable really the correct GravityBodyScript) or it hasn't been compiled yet (are there maybe other compilation errors preventing the file to be compiled?). $$anonymous$$aybe you can restart Unity and go through each file and check everything again. Also, if you would post all of the relevant code, we could help you spot potential issues.
Answer by Xarbrough · Nov 11, 2017 at 12:49 AM
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. ;)
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 ^^
I am telling the script which script exactly that I am referring to.
VarGravityBodyScript = GameObject.FindGameObjectWithTag ("Planet").GetComponent<GravityBodyScript> ();
Then, I later use that variable that contains the script that I want to interact with.
VarGravityBodyScript.Attract (transform);
Why does this not make sense? I did replace the "RigidBody" with "transform".
Read the answer carefully. You probably should actually read everything carefully. The tutorial code has been uploaded to pastebin.
Again the "Attract" method is defined inside the "GravityAttractor" class, not the GravityBody class.
You have a "GravityBodyScript" reference inside your GravityBodyScript which makes no sense. You want to reference a "GravityAttractorScript". You really should remove all those "Script" suffixes as they just confuse and really aren't necessary. The tutorial doesn't has them as well.
$$anonymous$$y apologies, I thought you were saying that I wrote a bunch of the code in the wrong place, but I just confused the script that my script variable was holding with another script, After replacing that, everything is working as it should now. :)
EDIT: It seemed like it was working at first, but now the object bounces on the planet a few times before shooting off and either orbiting the planet or flying of into nowhere.
EDIT: Upon closer inspection, it seems that the object sinks into the planet a tiny bit, somehow bypassing the collision mask of the planet, but then it remembers the collision mask and teleports about five times its own height into the air, then sinks back down and repeats this, but does bigger "bounces" each time.
Answer by Paul-Sinnett · Nov 11, 2017 at 12:48 AM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220612142701im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Function won't be called 1 Answer
Toggle Gravity on button press 1 Answer
How to call all methods of specific name ? 1 Answer
2 minor gameplay problems 0 Answers
local Physics 0 Answers