0
\$\begingroup\$

I am trying to instantiate my fire particle system during runtime like this:

GameObject fire, firePrefab;

void Start()
{
    ...
    firePrefab = Resources.Load("FirePrefab") as GameObject;
}

void FixedUpdate()
{
   if (Input.GetKeyDown(KeyCode.V))
   {
       fire = Instantiate(firePrefab, transform.GetChild(3).position, transform.GetChild(3).rotation);
       Destroy(fire, 5.0f);
   }
}

My FirePrefab is just a regular particle system, and the model the above script is attached to looks like this:

Model

Default

Camera

DroneFire

However, my fire prefab isn't instantiated anywhere when I run the game.

\$\endgroup\$
  • \$\begingroup\$ Any errors? Did you check if fireprefab is actually loaded and not null? \$\endgroup\$ – ElDuderino Sep 10 '17 at 23:39
1
\$\begingroup\$

GetChild is zero-based. to get the first object, "Default", you would call GetChild(0). In your case, to get "DroneFire", you should call GetChild(2), not GetChild(3) which will throw an exception before the instantiation can occur, because no such child exists. This exception should be fairly obvious in the console output. I would recommend looking in the console regularly when things don't work to find where things are failing.

fire = Instantiate(firePrefab, transform.GetChild(2).position, transform.GetChild(2).rotation)

As a side note, polling for input should generally not be done in FixedUpdate. Change to Update instead.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.