- Home /
GetComponent don't work (i am desperate)
Hi guys, i need you help.
I am working on a script that should assign "OggettoDaMuovere" (Move an Object) when it is instantieted. This is the code:
using UnityEngine;
using System.Collections;
public class RetroGameHandler : MonoBehaviour {
public GameObject ret;
public GameObject tri;
public GameObject tri90;
public GameObject maincamera;
float time2= 1.1f;
float time;
int rnd;
int contaOggetti= 1;
float conta= 0f;
GameObject[] ListaOggetti= new GameObject[100];
// Use this for initialization
void Start () {
time = 5;
}
// Update is called once per frame
void Update () {
time = time - Time.deltaTime;
time2 = time2 - Time.deltaTime;
if (time2 <= 0.0f) {
time2 = 1f;
conta = conta + 0.15f;
}
if (time <= 0) {
time=5f - conta;
if (time<=1f) time= 1f;
rnd= Random.Range(1,4);
switch (rnd) {
case 1:
ListaOggetti[contaOggetti] = Instantiate (ret, new Vector3 (GameObject.Find ("Player").transform.position.x,
GameObject.Find ("Player").transform.position.y + 20.0f), Quaternion.identity) as GameObject;
break;
case 2:
ListaOggetti[contaOggetti]= Instantiate (tri90, new Vector3 (GameObject.Find ("Player").transform.position.x,
GameObject.Find ("Player").transform.position.y + 20.0f), Quaternion.identity) as GameObject;
break;
case 3:
ListaOggetti[contaOggetti]= Instantiate (tri, new Vector3 (GameObject.Find ("Player").transform.position.x,
GameObject.Find ("Player").transform.position.y + 20.0f), Quaternion.identity) as GameObject;
break;
}
maincamera.GetComponent<GuiOggetti>().OggettoDaMuovere= ListaOggetti[contaOggetti];
contaOggetti= contaOggetti +1;
}
}
}
I don't find the error, it works for the first 2-3 time, then it don't.
Here is the code of the second script (hope it can help)
using UnityEngine;
using System.Collections;
public class GuiOggetti : MonoBehaviour {
public Texture2D avanti;
public Texture2D indietro;
public GameObject OggettoDaMuovere;
float speed = 4.0f;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKey (KeyCode.W)) {
OggettoDaMuovere.transform.Translate (Vector3.right * speed * Time.deltaTime);
}
if (Input.GetKey (KeyCode.Q)) {
OggettoDaMuovere.transform.Translate (Vector3.left * speed * Time.deltaTime);
}
Rect w2Rect = new Rect (Screen.width - Screen.width / 8.3f, Screen.height-Screen.height/1.03f, Screen.height / 6, Screen.height / 6);
Rect a2Rect = new Rect (Screen.width / 10 - Screen.width / 12, Screen.height-Screen.height/1.03f, Screen.height / 6, Screen.height / 6);
foreach (Touch t in Input.touches) {
Vector2 vec = t.position;
vec.y = Screen.height - vec.y;
if (w2Rect.Contains (vec)) {
OggettoDaMuovere.transform.Translate (Vector3.right * speed * Time.deltaTime);
}
if (a2Rect.Contains (vec)) {
OggettoDaMuovere.transform.Translate (Vector3.left * speed * Time.deltaTime);
}
}
}
void OnGUI() {
GUI.Button (new Rect (Screen.width - Screen.width / 8.3f, Screen.height-Screen.height/1.03f, Screen.height / 6, Screen.height / 6), new GUIContent (avanti, "Button 1"));
GUI.Button (new Rect (Screen.width / 10 - Screen.width / 12, Screen.height-Screen.height/1.03f , Screen.height / 6, Screen.height / 6), new GUIContent (indietro, "Button 2"));
}
Thank you if you will help me
I dont see anything wrong with your code at a glance, but if you say it works only sometimes, the only reason I can think of is that one of the 3 switch cases are broken somehow.
I suggest you Console.Log(rnd);
right before the Switch(rnd); and see if it breaks on any specific case. If so, you'll know where the problem is more exactly.
Finally, in the future, you should try to code in english, as that will make it easier for people to understand code, whenever you need help :)
I try your suggestion on my code. rnd is ok, the problem is in somewhere the case
The error appear in the 3 or 4 or 5 spawn, and it persist after he appear. Can be a bug of unity?
I doubt this is a unity bug. I think refactoring your time system may help, but it could easily be a problem with one of the prefabs. What is actually going wrong? Does the code just stop? Is there an error message?
Side note. You should not be using GameObject.Find so many times. It's very inefficient. Even more so that you are using it twice in the same line to find the same object.
If Player doesn't change set a variable at the beginning and use GameObject.Find once at the start.
Guys i have really no idea what to do. I create a variable player at star and assign it to Gameobject.find("Player") but it still don't work.
I have no error, no error message, i can't just move object. I really have no idea of what to do
P.S I can't help myself with debug cause GetComponent is not supported by the debugger
Also I guess that the GuiOggetti of the main camera doesn't change does it? In that case, you should get it in the start function, assign its reference to a variable and use ins$$anonymous$$d of looking for it each frames. I think this would help you decipher whether it is the get component that doesn't work. If it changes often, then I find it a good debugging practice to break down the code in multiple lines. You could then use some code to check whether it is the get component function that crashed. It's always a good debugging practice, I$$anonymous$$HO, to include these kind of checks when using the GetComponent, Find, FindWithTag functionalities of Unity. Hope this helps...
Best