Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Dytdyt · Nov 28, 2014 at 07:51 PM · 2dphysicswheel

The name does not exist in the current context

Hi guys

I'm new to coding and unity, and i can't seem to find a solution to this problem by searching the web :(

My code is this: using UnityEngine; using System.Collections;

 public class Pump : MonoBehaviour {
 
     // Use this for initialization
     void Start () {
         GameObject wheel = GameObject.Find("Wheel1");
         WheelControl wheelcontrol = wheel.GetComponent<WheelControl>();
         wheelcontrol.wheeltouch=0;
     }
     
     // Update is called once per frame
     void Update () {
         if(wheelcontrol.wheeltouch==1){  // <---- the problem. Neither wheel control or wheel touch exist in the current context
         if (Input.GetKey ("space"))
                         rigidbody2D.AddForce(Vector2.up * -10);
     if (Input.GetKeyUp ("space"))
             rigidbody2D.AddForce(Vector2.up * 2000);
         }
     }
 }
 

This is my other code:

 using UnityEngine;
 using System.Collections;
 
 public class WheelControl : MonoBehaviour {
     public float wheeltouch = 0;
     // Use this for initialization
     void Start () {
     
     }
     
     // Update is called once per frame
     void Update () {
         if (Input.GetKey("down"))
             rigidbody2D.AddTorque(200,0);
             //transform.Rotate(0, 0, 100);}
     }
     void OnCollisionEnter2D(Collision2D collision) {
         if (collision.gameObject.tag == "Finish"){
             wheeltouch=1;
         }
 
     }
     void OnCollisionExit2D(Collision2D collisionInfo) {
         if (collisionInfo.gameObject.tag == "Finish"){
             wheeltouch=0;
         }
     }
 }
 



Thanks in advance :-)))

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
0
Best Answer

Answer by zharik86 · Nov 28, 2014 at 07:53 PM

Create your variable "wheelcontrol" in first script as global:

  private WheelControl wheelcontrol = null;

  void Start () {
   GameObject wheel = GameObject.Find("Wheel1");
   wheelcontrol = wheel.GetComponent<WheelControl>();
   wheelcontrol.wheeltouch=0;
  }
  //And etc

I hope that it will help you.

Comment
Add comment · Show 5 · 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 Dytdyt · Nov 28, 2014 at 10:04 PM 0
Share

Thanks!! That solved the problem :)

But new problem now: using UnityEngine; using System.Collections;

 public class Pump : $$anonymous$$onoBehaviour {
     private WheelControl wheelcontrol = null;
 
     // Use this for initialization
     void Start () {
         GameObject wheel = GameObject.Find("Wheel1");
         WheelControl wheelcontrol = wheel.GetComponent<WheelControl>();
         wheelcontrol.wheeltouch=0;
     }
     
     // Update is called once per frame
     void Update () {
         if ((wheelcontrol.wheeltouch==1) && (Input.Get$$anonymous$$ey ("space"))){
             rigidbody2D.AddForce(Vector2.up * -10);}
 
         if ((wheelcontrol.wheeltouch==1) && (Input.Get$$anonymous$$eyUp ("space"))){
             rigidbody2D.AddForce(Vector2.up * 2000);}
         
     }
 }
 
 
 

Even though wheeltouch==1, then nothing happens when i press space. What could be the problem?? :(

avatar image zharik86 · Nov 29, 2014 at 07:39 PM 0
Share

First, check you variable "wheeltouch" using Debug and better use $$anonymous$$eyCode:

  void Update () {
   Debug.Log("$$anonymous$$yWheel == " + wheelcontrol.wheeltouch); //check variable
   if (wheelcontrol.wheeltouch == 1 && Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)) {
    rigidbody2D.AddForce(Vector2.up * -10);
   }
 
   if (wheelcontrol.wheeltouch == 1 && Input.Get$$anonymous$$eyUp ($$anonymous$$eyCode.Space)) {
    rigidbody2D.AddForce(Vector2.up * 2000);
   }
  }
avatar image Dytdyt · Nov 30, 2014 at 03:23 PM 0
Share

I get this "Object reference not set to an instance of an object" if that helps?

avatar image zharik86 · Nov 30, 2014 at 07:34 PM 0
Share

Are you add component by name "rigidbody2D" at your object, which have this script?

avatar image Dytdyt · Nov 30, 2014 at 10:03 PM 0
Share

I solved the problem own my own by changing a few things. But thanks for the help so far!

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

2 People are following this question.

avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

2D Joint/Wheel Question 1 Answer

Objects stuck dragging across floor 1 Answer

Calculate initial velocity for projectile to reach point at a given angle and calculate angle to reach point at a given velocity. 1 Answer

When I insert Folder images weight 120 MB "Library" increase of 1.98 GB 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