- Home /
Problem for moving an object with a button
Hye
I try to move an object only if i use "steroid" but the i have this error :
NullReferenceException: Object reference not set to an instance of an object Interact.OnGUI () (at Assets/Script/Interact.cs:57)
I'm a beginner, i make this code with some differents tutorials
This is my code
using UnityEngine; using System.Collections;
public class Interact: MonoBehaviour {
public GUIText texteTarget;
private bool selected = false;
public Transform target;
public bool steroide;
public Event e = Event.current;
public float steroideTimer;
public float coolDown;
// Use this for initialization
void Start ()
{
steroideTimer = 0;
coolDown = 2.0f;
steroide= false;
}
// Update is called once per frame
void Update ()
{
renderer.material.color = Color.white;
selected = false;
if(steroideTimer > 0)
{
steroideTimer -= Time.deltaTime;
}
if (steroideTimer < 0)
{
steroideTimer = 0;
}
if(Input.GetKeyUp("1"))
{
if(steroideTimer == 0)
{
Steroide();
steroideTimer = coolDown;
}
}
}
public void OnLookEnter()
{
renderer.material.color = Color.red;
texteTarget.text = "Press E to interact";
selected = true;
}
void OnGUI()
{
if(e.isKey && e.character == 'e' && selected)
{
if (steroide == true)
target.Translate(5,1,0);
}
}
void Steroide()
{
if(Input.GetKeyDown("1"))
{
steroide = true;
}
}
}
Your answer
Follow this Question
Related Questions
NullReferenceException: Object reference not set to an instance of an object CannonFire.Update 0 Answers
Program received signal: “SIGABRT” 0 Answers
NullReferenceException: Object reference not set to an instance of an object 1 Answer
Object reference not set to an instance of an object 1 Answer
Having trouble getting the sample code for Mathf.PerlinNoise to work. 1 Answer