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 /
avatar image
0
Question by Jared027 · Aug 17, 2018 at 11:59 PM · variablejointassign-variable

Says that variable has not been assigned(but I already did).

When I press play it gives me the error that the backWheel and frontWheel of CarController have not been assigned. I can't figure out why because I made 2 wheels as the child of my bike then I put Rigidbody 2ds on them with circle 2d colliders. The bike has a rb 2d with a polygon collider 2d and 2 wheel joint 2ds. The wheel joints both have the connected rigid body set as the front and the back wheel. Plus my CarController script also has the wheel joints set for back and front including the rigid body for the bike under the rb variable. I have no clue why this is occurring please help me.

 public float speed = 1500;
 public float rotationSpeed = 15f;

 private float movement = 0f;
 private float rotation = 0f;

 public WheelJoint2D backWheel;
 public WheelJoint2D frontWheel;

 public Rigidbody2D rb;
 
 void Update()
 {
    movement = -Input.GetAxisRaw("Vertical") * speed;
     Input.GetAxisRaw("Horizontal");
 }

 void FixedUpdate()
 {
     if (movement == 0f)
     {
         backWheel.useMotor = false;
         frontWheel.useMotor = false;
     }
     else
     {
         backWheel.useMotor = true;
         frontWheel.useMotor = true;
         JointMotor2D motor = new JointMotor2D { motorSpeed = movement, maxMotorTorque = 10000 };
         backWheel.motor = motor;
         frontWheel.motor = motor;
     }

     rb.AddTorque(-rotation * rotationSpeed * Time.fixedDeltaTime);  
 }

}

Comment
Add comment
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

1 Reply

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

Answer by rainChu · Aug 18, 2018 at 01:56 AM

Did you remember to assign them in the inspector?

You need to drag and drop the GameObject that has the RigidBody2D component attached to it in the Inspector window. If this car is a Prefab, you should also be sure to press the Apply button, or it will only affect the instance in the scene. Anything shown in bold in the Inspector is unique to that instance, and isn't going to be copied across your game. Example


Edit 1:

I think I see one possibility. You have two wheel joints, but they are both attached to the same GameObject. In order to build references to these two, you have to use GetComponents() inside there. Try this, instead:

 using UnityEngine.Assertions; // This helps us track bugs easier
 
 class CarController
 {
   [SerializeField] private GameObject m_wheelJointObject;
 
   // We're using auto properties, so as not to allow other scripts to modify them on accident
   public WheelJoint2D backWheel {get; private set;}
   public WheelJoint2D frontWheel {get; private set;}
 
   private void Awake( )
   {
     Assert.IsNotNull( m_wheelJointObject, "There is no object with wheel joints." );
 
     var wheelJoints = m_wheelJointObject.GetComponents<WheelJoint2D>();
 
     backWheel = wheelJoints[0]; // You may need to swap these. It depends on the order in the Inspector. Use the cog icon to move components up or down if you want to swap them there.
     frontWheel = wheelJoints[1];
   }
 }

Disclaimer: I didn't actually run this code, so I hope there are no typos!


Edit 2: So you've assigned that GameObject, as I can see in your screenshot. But that Assert.IsNotNull I slipped in there is telling us quite plainly that it's not set. This leads me to guess that there's another CarController in your scene that you're not setting up. Is there another GameObject you added and maybe forgot about? Or perhaps you added a CarController to something on complete accident? To find out which object is logging these errors, add this line to Awake():

 private void Awake( )
 {
   // Add this line to the top!
   Assert.IsNotNull( m_wheelJointObject, "The following object is not fully set up: " + name );

   // ... more existing code
 }

I hope this helps track it down! Good luck!

Comment
Add comment · Show 8 · 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 Jared027 · Aug 18, 2018 at 02:44 AM 0
Share

Yea I did drag it in the inspector.

avatar image Jared027 · Aug 18, 2018 at 02:50 AM 0
Share

alt text

coding.png (10.7 kB)
avatar image Jared027 · Aug 18, 2018 at 02:50 AM 0
Share

alt text

code.png (41.4 kB)
avatar image rainChu · Aug 18, 2018 at 03:05 AM 0
Share

I've edited the answer. I hope this one helps!

avatar image Jared027 · Aug 18, 2018 at 03:21 AM 0
Share

I have only been getting an error about one thing it says that the m_wheelJointobject of car controller has not been assigned and that it is nullalt text

dadd.png (88.6 kB)
avatar image rainChu Jared027 · Aug 18, 2018 at 03:29 AM 0
Share

We're getting closer now. Check the answer for an update! Good luck!

Show more comments

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

95 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

Related Questions

How to call variable from java script to c# Script? :( 2 Answers

Is there a way to make a variable correspond to multiple different scripts? 1 Answer

How to assign a private transform of my player in script? 1 Answer

Variable doesn't change in Unity methods 0 Answers

On Click, increment variable 1 Answer


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