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

Using the right particle instance

Discussion in 'Scripting' started by tdneren2, Jun 24, 2014.

  1. tdneren2

    tdneren2

    Joined:
    Sep 29, 2013
    Posts:
    14
    Hi - I can't seem to figure out how to instantiate the right particle system through:

    using UnityEngine;
    using System.Collections;

    public class RotateAround : MonoBehaviour {
    //public float dTimer = 0.0f;
    //public Vector3 zeroPoint = new Vector3(0, 0, 10);
    //public bool mouseDo =false;
    // Use this for initialization

    ParticleSystem partSys1 = new ParticleSystem();

    void Awake()
    {
    partSys1.transform.position = new Vector3(0, 0, 8-guis.hSliderValue);
    partSys1.emissionRate = 100;

    }

    void Start () {
    transform.position = new Vector3(0, 0, 8);
    }

    // Update is called once per frame
    void Update () {
    //if(mouseDo==false){
    // transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);}
    //transform.RotateAround(Vector3.zero, Vector3.up*guis.hSliderValue, 20 * Time.deltaTime);
    /*if(dTimer>0.9f){
    *
    transform.position = new Vector3(transform.position.x -guis.hSliderValue, transform.position.y, transform.position.z-guis.hSliderValue);
    print("GlobalVar:"+guis.hSliderValue);}
    if(dTimer<1.0f){dTimer+=Time.deltaTime;}
    else{dTimer = 0.0f;}
    print ("dTimer"+dTimer);*/
    if(Input.GetMouseButtonUp(0)){
    transform.position = new Vector3(0, 0, 8-guis.hSliderValue);}
    //if(Input.GetMouseButtonDown(0)){transform.position = new Vector3(transform.position.x, 0, transform.position.z);print("mouseDo =true;");}
    transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);

    }
    }


    - where the important stuff is inside the Awake-function. Al though I have a particle system by the name of ParticleSystem present in my 'Project folder', I get a NullRef error msg. I also did find it a problem that the particle system is already present in my heirachy, so I made it a prefab -> still the same result
     
  2. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
  3. zDemonhunter99

    zDemonhunter99

    Joined:
    Apr 23, 2014
    Posts:
    478
  4. tdneren2

    tdneren2

    Joined:
    Sep 29, 2013
    Posts:
    14

    .... ok, tried it sort of -> this beneath states my trouble:
    Code (csharp):
    1.  
    2.     public ParticleSystem partSys1;
    3.    
    4.     //ParticleSystem partSys1 = new ParticleSystem();
    5.         //GameObject partSys1 = new ParticleSystem();
    6.    
    7.     void Awake()
    8.     {
    9.         Instantiate(partSys1, new Vector3(0, 0, -8), Quaternion.identity);
    10.         /*partSys1.AddComponent<Rigidbody>();
    11.         partSys1.transform.position = new Vector3(0, 0, 8-guis.hSliderValue);
    12.         partSys1.emissionRate = 100;    */ 
    13.     }
    14.  
    15.     void Start () {
    16.         transform.position = new Vector3(0, 0, -8);
    17.     }
    18.    
    19.     // Update is called once per frame
    20.     void Update () {
    21.         //if(mouseDo==false){
    22.         //  transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);}
    23.         //transform.RotateAround(Vector3.zero, Vector3.up*guis.hSliderValue, 20  * Time.deltaTime);
    24.         /*if(dTimer>0.9f){
    25.          *
    26.         transform.position = new Vector3(transform.position.x -guis.hSliderValue, transform.position.y, transform.position.z-guis.hSliderValue);
    27.         print("GlobalVar:"+guis.hSliderValue);}
    28.         if(dTimer<1.0f){dTimer+=Time.deltaTime;}
    29.         else{dTimer = 0.0f;}
    30.         print ("dTimer"+dTimer);*/
    31.         if(Input.GetMouseButtonUp(0)){
    32.             transform.position = new Vector3(0, 0, -8+guis.hSliderValue);}
    33.         //if(Input.GetMouseButtonDown(0)){transform.position = new Vector3(transform.position.x, 0, transform.position.z);print("mouseDo =true;");}
    34.         transform.RotateAround(Vector3.zero, Vector3.up, 20 * Time.deltaTime);
    35.     }
    36.  
    37. [CODE]
    38.  
    39.  -> which gives me an error msg of, "The variable partSys1 has not been assigned"
     
  5. zaxvax

    zaxvax

    Joined:
    Jun 9, 2012
    Posts:
    220
    Yep, you need to assign that variable.
    Select your script in Project window.
    Now in Inspector window you have your script and there is a field for partSys1.
    Drag you ParticleSystem prefab from Project window into this field.

    P.S. Also you can do it with script that already has been assigned to a GameObject, or can assign it runtime, then you need to mess with Resources.Load
     
  6. tdneren2

    tdneren2

    Joined:
    Sep 29, 2013
    Posts:
    14

    Ok - did so....:

    Code (csharp):
    1.  
    2. public ParticleSystem instPS;
    3.    
    4.    
    5.    // Update is called once per frame
    6.    void Update () {
    7.  
    8.      instPS.Play();
    9.      instPS.transform.position = new Vector3(0, 0, 8-guis.hSliderValue);
    10.  
    11.    }
    12. [CODE]
    13.  
    14.  - but does still not work :-/ ... (tried the ressources.load also but got all sorts of problems with object-to-gameobject-to-transform issues)
    15.  
    16. The error msg I now get is: 'Object reference not set to an object ....'