Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by $$anonymous$$ · Nov 10, 2017 at 11:17 PM · scripting problemgravitymethod

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);
     }
 }
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Xarbrough · Nov 10, 2017 at 11:45 PM 0
Share

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

avatar image $$anonymous$$ Xarbrough · Nov 10, 2017 at 11:59 PM 0
Share

Yes to all of those things, my code basically looks like his code.

avatar image Xarbrough $$anonymous$$ · Nov 11, 2017 at 12:06 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

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 ^^

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image $$anonymous$$ · Nov 11, 2017 at 01:06 AM 0
Share

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

avatar image Bunny83 $$anonymous$$ · Nov 11, 2017 at 01:31 AM 0
Share

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.

avatar image $$anonymous$$ Bunny83 · Nov 11, 2017 at 02:19 AM 0
Share

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

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

128 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges