Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Making particles stay at its parent (MuzzelFlash)

Discussion in 'Scripting' started by Rutenis, May 15, 2014.

  1. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    Hey, im having this problem, when i shoot, muzzel flash doesnt stay at its position and i run threw it. I dont know how to fix it, here's the little script, that i wrote. Im having a Muzzel prefab and im instantiating it. maybe you can help, please? :p


    using UnityEngine;
    using System.Collections;

    public class Shootings : MonoBehaviour {

    public Transform Spawn;
    public Transform muzzelFlash;


    void Update ()
    {

    if (Input.GetButtonDown("Fire1"))
    {
    Instantiate(muzzelFlash, Spawn.position, Spawn.transform.rotation);
    }

    }
     
  2. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    turn this:
    Code (csharp):
    1.  
    2. if (Input.GetButtonDown("Fire1"))
    3. {
    4. Instantiate(muzzelFlash, Spawn.position, Spawn.transform.rotation);
    5. }
    6.  
    into
    Code (csharp):
    1.  
    2. if (Input.GetButtonDown("Fire1"))
    3. {
    4. GameObject muzzle = Instantiate(muzzelFlash, Spawn.position, Spawn.transform.rotation) as GameObject;
    5. muzzle.transform.parent = Spawn;
    6. }
    7.  
     
  3. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297
    I tried it, but i always get this error: NullReferenceException: Object reference not set to an instance of an object
    Shootings.Update () Maybe you know whats wrong?
     
  4. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    make sure muzzleFlash and Spawn are set in the inspector in Unity, before running the project/game.
     
  5. Rutenis

    Rutenis

    Joined:
    Aug 7, 2013
    Posts:
    297

    $testt.png

    Is there something wrong? I did everything good in the inspector. Thank you for helping. :confused:;)
     
  6. ar0nax

    ar0nax

    Joined:
    May 26, 2011
    Posts:
    485
    try adding "GameObject." before Instantiate();