- Home /
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 :-)))
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.
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?? :(
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);
}
}
I get this "Object reference not set to an instance of an object" if that helps?
Are you add component by name "rigidbody2D" at your object, which have this script?
I solved the problem own my own by changing a few things. But thanks for the help so far!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
2D Joint/Wheel Question 1 Answer
Objects stuck dragging across floor 1 Answer
When I insert Folder images weight 120 MB "Library" increase of 1.98 GB 0 Answers