- Home /
Show object with tag after button press
Hi, I look in many posts here and try lot of options but nothing works. I have spaceship with script. It have child with tag "Plomien" (its Flame in Polish). I want to show that Flame when I go forward. Pls help. One by one i get error "NullReferenceExeption: Object Reference not set to an instance of an object".
using UnityEngine;
using System.Collections;
public class Sterowanie : MonoBehaviour {
public float predkosc;
public float maksymalnaPredkosc = 10f;
private float predkoscSkretu;
private float predkoscZwrotu;
private GameObject plomien;
// Use this for initialization
void Start () {
predkosc = 0;
predkoscSkretu = 0.2f;
predkoscZwrotu = 0.2f;
rigidbody.useGravity = false;
// GameObject.FindWithTag("Plomien").SetActive(true);
// plomien.activeSelf;
}
void FixedUpdate()
{
//stala predkosc zalezna od zmiennej predkosc
transform.Translate (0, 0, predkosc);
//przyśpieszanie i maksymalna predkosc
if (Input.GetKey(KeyCode.A) && predkosc < 10f) {
Debug.Log("dzialaA");
predkosc = predkosc+0.1f;
//HERE IS MY PROBLEM. PLS HELP!
plomien = GameObject.FindGameObjectWithTag("Plomien");
MeshRenderer p = plomien.GetComponent<MeshRenderer>();
p.enabled = true;
// GameObject[] f1 = GameObject.FindGameObjectsWithTag("Plomien");
// foreach(GameObject f in f1)
// {
// f.GetComponent<MeshRenderer>().enabled = true;
// }
}
//zwalnianianie i predkosc minimalna
if (Input.GetKey(KeyCode.Z) && predkosc > 0f) {
Debug.Log("dzialaZ");
predkosc = predkosc-0.1f;
}
//obrót w góre i dół zrobić dwoma punktami. Niech dziób unosi się w góre, a tył statku w dół
if (Input.GetKey(KeyCode.UpArrow)) {
transform.Rotate(Vector3.left * predkoscSkretu);
}
if (Input.GetKey(KeyCode.DownArrow)) {
transform.Rotate(Vector3.right * predkoscSkretu);
}
if (Input.GetKey(KeyCode.LeftArrow)) {
transform.Rotate(Vector3.forward * predkoscSkretu);
transform.Rotate(Vector3.down * predkoscZwrotu);
}
if (Input.GetKey(KeyCode.RightArrow)) {
transform.Rotate(Vector3.back * predkoscSkretu);
transform.Rotate(Vector3.up * predkoscZwrotu);
}
}
// Update is called once per frame
void Update () {
}
}
If it is a NullReferenceExeption probably the code isn't finding an object with the "Plomien" tag.
Check if it's spelled correctly, and also use some Debug.Log to check what the "FindGameObjectWithTag" returns.
Debug Log give me Null. Spelling is right. If i change tag, I get error that unity didnt found object with this tag, so it can find this but see only Null.
http://ifotos.pl/zobacz/picturepn_enwsrse.png/
pls help, if you have better solution to do this, give me some.
Again, as Vitor_r mentioned. A nullrefernce means that it has not found the object. The reference is missing ("The link"). Just like you create a shortcut on your desktop to: X:\Program Files
ins$$anonymous$$d of C:\Program Files
. X:\Program Files
doesnt exist so the reference("the link/connection") is missing
So if i show here code and screen how it looks in unity, can someone give me example how to create good reference by tag, and how to correctly create gameobject.findwithtag("");?
Your answer
Follow this Question
Related Questions
Trouble Creating a Procedural Mesh on a Rotated Game Object 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Cutting a Mesh Into Smaller Pieces 5 Answers
Problems with Transform.Find and GameObject.find 1 Answer
How to Change an Object's Mesh to another Object's Mesh via Code C# 1 Answer