- Home /
FindObjectsOfType doesn't find instantiated objects
I'm making a pipe connecting game and I have an object that spawns pipes on click. I have another object(valve) that starts the water and I need this object to find all the pipe connectors. This valve uses FindObjectsOfType and puts them in an array of type connector. This works fine for the objects already in the scene, that is for objects that are there before I start the game. But it doesn't work for pipes I instantiate. This is the code for the valve:
using UnityEngine;
using System.Collections;
public class RotirajVentil : MonoBehaviour {
public GameObject izvor;
private int smjer=-1;
private bool pustiGas = false;
public Veza1[] sveVeze1;
public Veza2[] sveVeze2;
// Use this for initialization
void Start () {
sveVeze1= FindObjectsOfType<Veza1> ();
sveVeze2= FindObjectsOfType<Veza2> ();
}
// Update is called once per frame
void Update () {
}
void OnMouseOver(){
if(Input.GetMouseButtonDown(1)){
transform.Rotate(new Vector3(0,0,smjer*90));
pustiGas = !pustiGas;
smjer = -smjer;
Pusti (pustiGas);
}
}
void Pusti(bool da)
{
if(da)
{
izvor.GetComponent<ParticleSystem> ().Play ();
for(int i=0;i<sveVeze1.Length;i++)
{
sveVeze1 [i].pustena = true;
sveVeze2 [i].pustena = true;
sveVeze1 [i].Pustaj ();
sveVeze2 [i].Pustaj ();
}
}
else
{
izvor.GetComponent<ParticleSystem> ().Stop ();
for(int i=0;i<sveVeze1.Length;i++)
{
sveVeze1 [i].pustena = false;
sveVeze2 [i].pustena = false;
sveVeze1 [i].Pustaj ();
sveVeze2 [i].Pustaj ();
}
}
}
}
Veza1 and Veza2 ar the connectors of a pipe and are themselves a c#script.
This is the code I instantiate the pipes with:
using UnityEngine;
using System.Collections;
public class InstancirajVoduL : MonoBehaviour {
public GameObject VodaL;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnMouseOver ()
{
if (Input.GetMouseButtonDown (2))
{
Instantiate (VodaL, transform.position+(transform.up*-40)+(-transform.forward*10), VodaL.GetComponent<Transform>().rotation);
}
}
}
VodaL is the prefab that has Veza1 and Veza2 as its children.
If anyone can help me I would be very grateful.
Solved it. It was stupid, I didin't call the function from update so it couldn't possibly find instantitated objects that appear after creation of the valve.
Your answer
Follow this Question
Related Questions
Script error 1 Answer
Help on scripting a ball 2 Answers
Animation script gravity problem 0 Answers
How would i go about comparing two transform scales 1 Answer
Collision Detecting not working! 1 Answer