Search Unity

Referencing other objects without Triggers.

Discussion in 'Scripting' started by ChrisX, Mar 4, 2015.

  1. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    Hello all.

    As a beginner, I'm having trouble about passing away some objects here. Here's the basic rundown:

    I have 3 Objects, we will say they are Player, Object and Particle. The condition is that if the Player collides with the Object, first, four Particles are created in the same position as the Object, then they're supposed to move independently (though automatic) as the Object is destroyed.

    The Particles are created within the Object script, we call it ObjectController.
    Code (CSharp):
    1. void InitiateClones(){
    2. //Clones are GameObjects
    3.         Vector3 particlePos = transform.position;
    4.         clone = Instantiate(particles, particlePos, Quaternion.identity) as GameObject;
    5.         clone2 = Instantiate(particles, particlePos, Quaternion.identity) as GameObject;
    6.         clone3 = Instantiate(particles, particlePos, Quaternion.identity) as GameObject;
    7.         clone4 = Instantiate(particles, particlePos, Quaternion.identity) as GameObject;
    8.         clone.tag = "Particle";
    9.         clone2.tag = "Particle2";
    10.         clone3.tag = "Particle3";
    11.         clone4.tag = "Particle4";
    12.  
    13.     }
    In here, I want to call out a method in the Particles, which will bring over the clone game objects to be scripted in the Particles script, we'll say ParticleController. What would be the script I need?

    Thanks.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,836
    I'm not sure precisely what you're asking.

    If you've written your own component (MonoBehavior) and attached it to whatever prefab you're using to create the particles, then you can invoke methods on that component with something like
    Code (CSharp):
    1. clone.GetComponent<YourComponentName>().YourMethodName();
    If there's some other class that you already have a reference to, and you want that class to be able to do things with your particles, you can pass your particles as the arguments to some method call on that class.
    Code (CSharp):
    1. particleController.DoSomething(clone, clone2, clone3, clone4);
    Does that help?
     
  3. ChrisX

    ChrisX

    Joined:
    Feb 13, 2015
    Posts:
    63
    That helps, though I thought in the end, better use SendMessage, because you don't need to modify the scope of the clone on the method.

    Thanks!