Search Unity

Question How to sent the transform value of a Gameobject to another script?

Discussion in 'Scripting' started by SoloDevCsharp, Jun 29, 2021.

  1. SoloDevCsharp

    SoloDevCsharp

    Joined:
    Mar 5, 2021
    Posts:
    7
    A decal bullet hole is instantiated on a gameobject when I shoot. How can I pass the value of the transform of the bullet hole to another script? The bullet hole is set to bind with the object being shoot at. My code is the following.

    if (hit.transform.tag == "DecalMove")
    {
    var decalHole = Instantiate(hitParticles, hit.point, Quaternion.FromToRotation(Vector3.up, hit.normal)) as GameObject;
    decalHole.transform.parent = hit.transform;
    }

    Thanks!
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,742
    Are you asking how to pass an argument to another function?

    For that, please check out basic C# syntax.

    Or just how to get at functions in other script instances?

    For that it's always the same in Unity:

    Referencing variables, fields, methods (anything non-static) in other script instances:

    https://forum.unity.com/threads/hel...-vars-in-another-script.1076825/#post-6944639

    https://forum.unity.com/threads/accessing-a-gameobject-in-different-scene.1103239/

    EDIT: looking at your construct above you probably want to wrap that up in a decal-maker script, using some kind of pattern like this. Otherwise you might have copies of this scattered all over your code.

    Factory Pattern in lieu of AddComponent (for timing and dependency correctness):

    https://gist.github.com/kurtdekker/5dbd1d30890c3905adddf4e7ba2b8580
     
    SoloDevCsharp and Brathnann like this.
  3. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,188
    What do you mean by passing the transform? Are you wanting to grab a position of something? Or just pass a value? We'd have to see a bit more of your scripts to give you a better idea, but you seem to be on the right track. You've both instantiated something and grabbed the transform of something you've hit.

    decalHole.transform is the transform of what I assume is a bullet hole. So, now you just need a reference to whatever script you want to send it to and send it over. If it's a script on the hit target, then you could just do.

    Code (CSharp):
    1. hit.transform.GetComponent<SomeOtherScript>().TakeMyTransform(decalHole.transform);
     
    SoloDevCsharp likes this.
  4. John_Leorid

    John_Leorid

    Joined:
    Nov 5, 2012
    Posts:
    651
    Best System for Bullet Decals and their Particles I've seen so far was with ScriptableObjects. You create one for each type of Impact (WoodImpact, FleshImpact, StoneImpact, GrassImpact, ..).
    Then you need to assign those to the corresponding Objects, add the WoodImpact to wood and so on.
    Each impact has a decal texture and a particle system.
    If your Bullet hits such an object, it checks for its type (a component with a reference to the correct Impact) and then passes this impact to a static class which will spawn the decal and particles.

    Whatever you do, I would use a static class. And yes, you can pass the transform to a static method of the static class.