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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

What can i write to component in function.

Discussion in 'Scripting' started by emrah-gerede, Jun 10, 2020.

  1. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Hi,
    what can i write for component when i create a function?

    Example

    Public void test (.... ex)
    {
    gameObject. GetComponent<ex>() ;
    }

    Instead of .... ????

    When i was write Component instead of... I taking a mistake.

    'ex' is a variable but is use like type.

    What can i do about this situation?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    It's unclear what you are trying to do exactly. Maybe you are trying to create a generic method that takes a type? Is this what you are looking for? Sorry your English is difficult to understand.

    Code (CSharp):
    1. public void test<T>() where T : Component
    2. {
    3.   gameObject.GetComponent<T>() ;
    4. }
     
    emrah-gerede and dahiyabunty1 like this.
  3. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    praetor can u help me understand how to use this where to generic method
     
  4. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    Well this method doesn't really do anything. I have no idea what the OP is actually trying to accomplish. I guess one possibility would be something like this:
    Code (CSharp):
    1. public bool HasComponent<T>() where T : Component {
    2.   T behaviour = GetComponent<T>();
    3.   return behaviour != null;
    4. }
    Then elsewhere in your code you could check for the existence of a type of component:
    Code (CSharp):
    1. if (HasComponent<Collider>()) {
    2.   // do something
    3. }
    You could probably achieve something more useful with a more concrete subtype like Behaviour which would let you enable/disable something, or some custom type.
     
    emrah-gerede and dahiyabunty1 like this.
  5. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Sorry about that.
    I have writed my phone.

    I want to explain again.

    Example you have got a simple function.

    Public void test(string k) {
    Debug. Log(k);
    }

    If you want to call this function you write test("hello my name is " ) ;


    Well if my function is

    Public void test ( ???) {

    gameObject. GetComponent<test>() ;



    }

    Test(????)
     
  6. dahiyabunty1

    dahiyabunty1

    Joined:
    Jan 22, 2020
    Posts:
    68
    thnks i will google concrete subtypes
     
  7. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    your function doesn't do anything. we need to know what you're trying to get from the function.
    this function takes a gameobject and checks if it has a script attached

    run it with
    test(cube_gameobject);

    Code (CSharp):
    1. Public void test (GameObject obj) {
    2.     if(obj.gameObject.GetComponent<somescript>() )
    3.     {
    4.         Debug.Log("the object has the script");
    5.     }else
    6.     {
    7.         Debug.Log("script not found");
    8.     }
    9.  
    10. }
     
    emrah-gerede likes this.
  8. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Johne5, what i can write instead of ???.
     
  9. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    you could put this.gameObject
    so
    test(this.gameObject);

    or if you have public gameObject, you can use that.

    public GameObject testObject;

    test(testObject);
     
    emrah-gerede likes this.
  10. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    So;
    Public void test(GameObject ex) // what can i write instead of GameObject
    {
    gameObject. GetComponent<ex>();
    }

    Test(b); // b is public gameobject

    Sorry, it is not working.
     
  11. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Error is
    Ex is a variable but is used like a type.
     
  12. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,725
    If you want a "dynamic" thing to put into GetComponent you have to do it like my example:
    Code (CSharp):
    1. public void test<T>() where T : Component
    2. {
    3.   gameObject.GetComponent<T>() ;
    4. }
    Then you can call it with whatever type you want. For example:
    Code (CSharp):
    1. test<Collider>();
     
    emrah-gerede likes this.
  13. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133

    You're giving us enough info about what you're trying to do.
    it's failing because of this line
    gameObject.GetComponent<ex>();
    This does nothing. it's an incomplete statement. you're getting a conponent and not doing anything with it.
    and it should be use like this.
    ex.GetComponent<Transform>(); or what ever class you're looking for

    GameObject can be any class.
    like
    Public void test(Text ex)
    Public void test(Animator ex)
    Public void test(Renderer ex)
    Public void test(Transform ex)
    Public void test(String ex)
    Public void test(Int ex)
    Public void test(Float ex)
     
    emrah-gerede likes this.
  14. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    My real function;

    Public void levelLov(Trabsform childA, string geciciString,........ diz) // what can i write instead of.....?
    {
    foreach (Transform child in childA)
    {
    if (child.name! =geciciString & & child. gameObject. GetComponent<diz>())
    {k=k+1;animArr[k]=child;}
    }
    etc.

    So, preatorblue Your codes is not proper to my codes.
     
  15. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I hope this will help

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. public class TestApp : MonoBehaviour
    4. {
    5.  
    6.     public GameObject ex;
    7.     private Transform tx;
    8.     void Start()
    9.     {
    10.         test1(ex);
    11.         test2("New Name For Object");
    12.     }
    13.     void Update()
    14.     {
    15.  
    16.     }
    17.    
    18.     void test1 (Gameobject obj)
    19.     {
    20.         tx = obj.GetComponent<Transform>();
    21.     }
    22.    
    23.     void test2 (string str)
    24.     {
    25.         ex.gameObject.name = str;
    26.     }
    27.  
    28. }
     
    emrah-gerede likes this.
  16. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Johne5 I know you are right but unity3d give an error when i did your say
     
  17. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    So the answer is lots of things.
    but from your code it looks like you might be trying to set an animation

    Code (CSharp):
    1. Public void levelLov(Trabsform childA, string geciciString, Animator diz)
    2. {
    3.     foreach (Transform child in childA)
    4.     {
    5.         if (child.name! = geciciString & & child.gameObject.GetComponent<diz>())
    6.         {
    7.             k=k+1;animArr[k]=child;
    8.         }
    9.     }
    10. }
    the line of code is looking for a script to be attached to the child objects. So, can you tell me the name of a script that is attached to a child object?
     
    emrah-gerede likes this.
  18. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    @PraetorBlue's solution can work here. What you probably want to do is this:

    Code (CSharp):
    1. public void LevelLov<T>(Transform child, string geciciString) where T : Component
    2. {
    3.    foreach (Transform child in childA)
    4.    {
    5.       if (child.name != geciciString && child.gameObject.GetComponent<T>() != null)
    6.       {
    7.          k=k+1;
    8.          animArr[k]=child;
    9.       }
    10.    }
    11. }
     
    emrah-gerede likes this.
  19. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Unfortunately same error :( johne5


    'diz' is a variable but is used like a type.
     
  20. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    I think you might have a typo.
    Public void levelLov(Trabsform childA, string geciciString, Animator diz)
    should be
    Public void levelLov(Transform childA, string geciciString, Animator diz)
     
    emrah-gerede likes this.
  21. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    No no it is a phone mistake johne5 code is right.

    PraetorBlue and vryken, i am trying your suggestion.
     
  22. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    can you tell us what script you're looking for in child objects?
    Does it change all the time? then try PraetorBlue's code.
     
    emrah-gerede likes this.
  23. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Well, how can i call this function?
    levelLov(tempTransform, tempString,....)
     
  24. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    it sounds like you don't know what diz is used for, so i removed it. give this a try
    Code (CSharp):
    1. Public void levelLov(Trabsform childA, string geciciString)
    2. {
    3.     foreach (Transform child in childA)
    4.     {
    5.         if (child.name! = geciciString)
    6.         {
    7.             k=k+1;
    8.             animArr[k]=child;
    9.         }
    10.     }
    11. }
    call it with
    levelLov(tempTransform, tempString)
     
    emrah-gerede likes this.
  25. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Johne5, there is a lot of objects in my scene and they have some components thats I write. Example A1 B1 etc.. (400-500 pieces)

    This function will make different animations based on the component on the object.
     
  26. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    ok, so go with something like this
    Code (CSharp):
    1. public void LevelLov<T>(Transform child, string geciciString) where T : Component
    2. {
    3.    foreach (Transform child in childA)
    4.    {
    5.       if (child.name != geciciString && child.gameObject.GetComponent<T>() != null)
    6.       {
    7.          k=k+1;
    8.          animArr[k]=child;
    9.       }
    10.    }
    11. }
    12.  
    13. call it with
    14. levelLov<B1>(tempTransform, tempString);
     
    emrah-gerede likes this.
  27. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    PraetorBlue and
    Your code is not giving an error but how can i call this function with 3 arguments
     
  28. Vryken

    Vryken

    Joined:
    Jan 23, 2018
    Posts:
    2,106
    Add a third argument to the function, whatever that may be.
     
    emrah-gerede likes this.
  29. emrah-gerede

    emrah-gerede

    Joined:
    Jun 24, 2016
    Posts:
    14
    Finally, it is working. You're amazing.
    Thank you for your answer.
    PraetorBlue, Johne5, Vryken

    You are my hero this month

    Thanks alot really
     
    PraetorBlue likes this.