Search Unity

Question How To Put Gameobject onto Object on awake

Discussion in 'Scripting' started by syb3rpunktv, Dec 9, 2021.

  1. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    So i need help im trying to get the audioSource from the scene to go onto the model on runtime the blendshapesproxy works fine bit i can't seem to get the audio source to go onto the model

    Code (CSharp):
    1.         public VRMBlendShapeProxy blendShapeProxy;
    2.         public OVRLipSyncContext lipsyncContext;
    3.         GameObject AudioSourceObject;
    4.  
    5. private void Awake()
    6.         {
    7.  
    8.  
    9.  
    10.             if (lipsyncContext == null) GameObject.Find("AudioSourceObject");
    11.             if (blendShapeProxy == null) blendShapeProxy = GetComponent<VRMBlendShapeProxy>();
    12.             {
    13.  
    14.             }
     
    Last edited: Dec 9, 2021
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,647
    Not really sure what you're trying to do, but you never set your found object to any variable. I'm assuming you meant to set
    lipsyncContext
    to it.

    And next time you use this forum, be sure to use Code Tags and try to describe your problem clearly, low effort posts are against the Community Code of Conduct. Quoted: "Posting threads and replies with little to no content will be removed without question. Please ensure that what you are posting has as much information as possible for other forum users to help or understand what you are saying."
     
  3. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18

    sorry I'm not really a coder I know how to edit limited code, so i don't really know what's going on but i give you all the code whats im trying to do is attach AudioSourceObject to the lipsyncContext and if no object exists just live it blank i tried GameObject.Find and GetComponent

    Code (CSharp):
    1.  
    2. using UnityEngine;
    3. using VRM;
    4.  
    5.  
    6. namespace VRM
    7. {
    8.     public class VRMLipSyncMorphTarget : MonoBehaviour
    9.     {
    10.         public VRMBlendShapeProxy blendShapeProxy;
    11.         public OVRLipSyncContext lipsyncContext;
    12.         GameObject AudioSourceObject;
    13.  
    14.         // smoothing amount
    15.         [Range(1, 100)]
    16.         [Tooltip("Smoothing of 1 will yield only the current predicted viseme, 100 will yield an extremely smooth viseme response.")]
    17.         public int smoothAmount = 10;
    18.  
    19.         OVRLipSync.Viseme[] visemes =
    20.         {
    21.         OVRLipSync.Viseme.aa,
    22.         OVRLipSync.Viseme.E,
    23.         OVRLipSync.Viseme.ih,
    24.         OVRLipSync.Viseme.oh,
    25.         OVRLipSync.Viseme.ou,
    26.     };
    27.  
    28.         void Start()
    29.         {
    30.             if (lipsyncContext == null)
    31.             {
    32.                 blendShapeProxy.GetComponent<VRMBlendShapeProxy>();
    33.  
    34.                 Debug.LogError("VRMLipSyncMorphTarget.Start Error: " +
    35.                     "No OVRLipSyncContext component on this object!");
    36.             }
    37.             else
    38.             {
    39.                 lipsyncContext.Smoothing = smoothAmount;
    40.             }
    41.         }
    42.  
    43.         private void Awake()
    44.         {
    45.  
    46.  
    47.  
    48.             if (lipsyncContext == null) GameObject.Find("AudioSourceObject");  //.GetComponent<AudioSource>();
    49.             if (blendShapeProxy == null) blendShapeProxy = GetComponent<VRMBlendShapeProxy>();
    50.             {
    51.  
    52.             }
    53.      
    54.             // get the current viseme frame
    55.             OVRLipSync.Frame frame = lipsyncContext.GetCurrentPhonemeFrame();
    56.             if (frame != null)
    57.             {
    58.                 SetVisemeToMorphTarget(frame);
    59.             }
    60.  
    61.             // Update smoothing value
    62.             if (smoothAmount != lipsyncContext.Smoothing)
    63.             {
    64.                 lipsyncContext.Smoothing = smoothAmount;
    65.             }
    66.         }
    67.  
    68.         void SetVisemeToMorphTarget(OVRLipSync.Frame frame)
    69.         {
    70.             foreach (OVRLipSync.Viseme viseme in visemes)
    71.             {
    72.                 int index = (int)viseme;
    73.                 switch (viseme)
    74.                 {
    75.                     case OVRLipSync.Viseme.aa:
    76.                         blendShapeProxy.SetValue(BlendShapePreset.A, frame.Visemes[index]);
    77.                         break;
    78.                     case OVRLipSync.Viseme.E:
    79.                         blendShapeProxy.SetValue(BlendShapePreset.E, frame.Visemes[index]);
    80.                         break;
    81.                     case OVRLipSync.Viseme.ih:
    82.                         blendShapeProxy.SetValue(BlendShapePreset.I, frame.Visemes[index]);
    83.                         break;
    84.                     case OVRLipSync.Viseme.oh:
    85.                         blendShapeProxy.SetValue(BlendShapePreset.O, frame.Visemes[index]);
    86.                         break;
    87.                     case OVRLipSync.Viseme.ou:
    88.                         blendShapeProxy.SetValue(BlendShapePreset.U, frame.Visemes[index]);
    89.                         break;
    90.                     default:
    91.                         blendShapeProxy.SetValue(BlendShapePreset.A, 0);
    92.                         blendShapeProxy.SetValue(BlendShapePreset.I, 0);
    93.                         blendShapeProxy.SetValue(BlendShapePreset.U, 0);
    94.                         blendShapeProxy.SetValue(BlendShapePreset.E, 0);
    95.                         blendShapeProxy.SetValue(BlendShapePreset.O, 0);
    96.                         break;
    97.                 }
    98.             }
    99.         }
    100.     }
    101. }
     
    Last edited: Dec 9, 2021
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Again, as already mentioned... These calls don't do anything for you.
    Code (CSharp):
    1. blendShapeProxy.GetComponent<VRMBlendShapeProxy>();
    2. GameObject.Find("AudioSourceObject");
    3.  
    It's like telling someone to go to the store, pick something off the shelf, and then put it back on the shelf. Sure, they found the object, but now what? While in your head you're thinking to buy it and bring it home, you didn't give those clear instructions.

    Funny thing is, you did this on line 49. See the difference? You told it to get something AND assign it to a variable.

    Code (CSharp):
    1.  blendShapeProxy = GetComponent<VRMBlendShapeProxy>();
    2.             {
    Might help to go into some basic tutorials. But hopefully this helps you understand what is wrong.
     
  5. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    so i may not be getting how this works i read the API reference i change

    Code (CSharp):
    1.  if (lipsyncContext == null) GameObject.Find("AudioSourceObject");  //.GetComponent<AudioSource>();
    2.  
    to


    Code (CSharp):
    1.             if (lipsyncContext == GameObject.Find("AudioSourceObject"));
    2.             {
    3.  
    4.             }
    5.  
    i dunno whey it will not look at lipsyncContext and add AudioSourceObject to it i tryed get component i also commented out


    Code (CSharp):
    1. /*
    2.         void Start()
    3.         {
    4.             if (lipsyncContext == null)
    5.             {
    6.              
    7.                 Debug.LogError("VRMLipSyncMorphTarget.Start Error: " +
    8.                     "No OVRLipSyncContext component on this object!");
    9.             }
    10.             else
    11.             {
    12.                 lipsyncContext.Smoothing = smoothAmount;
    13.             }
    14.         }
    15. */
    as its also at the end of the code
     
  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    I'm not sure what is confusing. GameObject.Find on it's own serves no purpose. It finds the object, but without doing anything with what it finds, it just tosses it away. You have to tell it what to do. Do you want it assigned to a variable? (Yeah, that's exactly what you're trying to do), then assign it to a variable. Which is exactly what you do with your GetComponent<VRMBlendShapeProxy>() call. You assign that to a variable. You have to do the same for your other two calls. GameObject.Find and your other GetComponent call both need to be assigned to a variable. And just make sure everything is being assigned to the proper variables.
     
  7. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    ya i can't find anything relating to what i am tring to do


    Code (CSharp):
    1.             //  GameObject.Find("AudioSourceObject");
    2.  
    3.             if (lipsyncContext == null)
    4.                 GameObject.Find("AudioSourceObject");
    5.            // lipsyncContext = GameObject.Find <AudioSourceObject>();
    6.             //lipsyncContext = gameObject.AddComponent<>();
    7.        
    8.  
    9.             //   lipsyncContext = GetComponent<OVRLipSyncContext>();
    10.             // gameObject.AddComponent<GameObject>();
    11.  
    12.  
    13.             //    gameObject.AddComponent<AudioSourceObject>();
    14.  
    15.             // lipsyncContext = GameObject.Find("AudioSourceObject");
    16.             // GetComponent.AudioSourceObject<>();
    17.             {
    18.                
    19.             }
    20.             // gameObject.AddComponent<AudioSourceObject>();
    21.             // lipsyncContext = GameObject.ReferenceEquals <AudioSourceObject>();
    all of them comes up for errors when i try and add the gameobject to a script Component runtime i am doing something wrong and not understanding how to get the gameobject that's already in the scene onto the script component when the model has loaded

    this is what I thought it would have been but no look


    Code (CSharp):
    1.             if (lipsyncContext == null)
    2.                 GameObject.Find("AudioSourceObject"); //To Find GameObject AudioSourceObject
    3. gameObject.AddComponent<AudioSourceObject>();    // Add GameObject To lipsyncContext Component
     
  8. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    What do you think this line does?

    Code (CSharp):
    1. GameObject.Find("AudioSourceObject");
     
  9. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    it finds the object in the scene this gameobject AudioSourceObject?
     
    Last edited: Dec 9, 2021
  10. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Correct. It finds the object. But after finding the object, what do you expect it should be doing with that object?
     
  11. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    Add the game object AudioSourceObject to it why I added gameObject.AddComponent<AudioSourceObject>()
     
  12. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    ok so none off them are going to work at getcomponent gets a component thats already on the model i need to get a gameobject in the scene and put it into a public variable at runtime
     
    Last edited: Dec 10, 2021
  13. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Nope, neither this line or the GameObject.Find line will add an AudioSourceObject to the found GameObject.

    This line finds a GameObject named AudioSourceObject, but because you don't do anything with it, it simply tosses it away. It says, hey, I found your gameobject but you didn't tell me to store a reference to it anywhere. So, I guess I don't need it. It does nothing else with it.
    Code (CSharp):
    1. GameObject.Find("AudioSourceObject");
    This line here actually adds the AudioSourceObject component to the SAME gameobject that has your VRMLipSyncMorphTarget script on it. The lowercase "gameObject" says to look at the same GameObject the script is on and do something with it.
    Code (CSharp):
    1. gameObject.AddComponent<AudioSourceObject>();
    When you call GameObject.Find you need to assign it to a variable. I'm trying to get you to understand how to do that yourself. You have this variable here which I assume is what you wanted to assign it to.

    Code (CSharp):
    1. GameObject AudioSourceObject;
    Then, when you want to add the component, you'll target that variable instead and not use "gameObject".
     
  14. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    ya i search google and everything this code but comes up with nothing but i tried this and nothing happens


    Code (CSharp):
    1.        
    2.                 GameObject.Find("AudioSourceObject");
    3.                 lipsyncContext = AudioSourceObject.AddComponent<OVRLipSyncContext>();
    if i try it the other way
    Code (CSharp):
    1. lipsyncContext = OVRLipSyncContext.AddComponent<AudioSourceObject>();
    [/code] it comes up with errors
     
  15. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Ok, you really need to go look at tutorials because you are still missing one very key thing that I said to do.

    GameObject.Find MUST assign what it finds to a variable. You must tell it to do so. You're still not doing that. So of course you'll get errors.

    Unity's own docs even give you an example.
    https://docs.unity3d.com/ScriptReference/GameObject.Find.html

    Unity's docs are pretty good overall in this sort of thing.
     
    Last edited: Dec 10, 2021
  16. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    so i got it to go onto my gameobject but its in not the right component it makes its own component


    Code (CSharp):
    1. public GameObject AudioSourceObject;
    2.  
    3. private void Awake()
    4.         {
    5.             if (lipsyncContext == null) AudioSourceObject = GameObject.Find("AudioSourceObject");
    6.  
    7. }
     
  17. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    You are one step closer. But idk what you mean by makes it's own component. If the gameobject "AudioSourceObject" already has components on it, then they are still going to be on it. You can add additional components with the AddComponent call and targetting the AudioSourceObject if you want to add another component.
     
  18. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    it just makes its own variable called AudioSourceObject in the inspector on play instead of putting the AudioSourceObject into the lipsyncContext variable
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,452
    This thread is confusing. It really does seem like you're guessing how things work and need to take a step back away from your problem above to learn some basics such as what GameObjects, Components, C# references are and how you reference them in fields and variables etc.

    A GameObject has Components. You can add, delete or retrieve (get) components that are on a GameObject. You can find GameObject by name even though it's super inefficient to do that. A "variable" as you call it is known as a field in C# and there are things known as "Reference" types which are essentially classes like GameObject or anything that's a Unity component.

    It's either the basics above or you're incorrectly assuming which GameObject have which Components.
     
  20. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    yes I'm guessing I have googled and can't find anything about getting a gameobject into a variable in a script at runtime
     
  21. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,452
    Confused. That's what "GameObject myVariable = GameObject.Find(name);" is doing where "name" is the string name of the GameObject you want (as it appears in the hierarchy).

    I think your misunderstanding runs much deeper.
     
  22. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    i did this but i does nothing


    Code (CSharp):
    1. lipsyncContext = GameObject.Find("AudioSourceObject");
    2.  
    3. also tryed
    4.  
    5. GameObject lipsyncContext = GameObject.Find("AudioSourceObject");
    beacuse the top one to me i probley missunderstand this script says that lipsyncContext is equal to Gameobject AudioSourceObject thats in the scene
     
  23. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    Both of these find a Gameobject in the scene that is named AudioSourceObject and assign it to the variable lipsyncContext. The difference is the scope of the variables. The top one could be a field for the class while the bottom one would be a local or method variable. So, they both accomplish assigning a GameObject to a variable.
     
  24. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    hmmm ok seems like it not assining anything to it must be something else in the script stoping it then
     
  25. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    If lipsyncContext is null after the GameObject.Find call, then it can't find a GameObject named AudioSourceObject.
     
    MelvMay likes this.
  26. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,452
    Adding to the above, can you show us an image of a GameObject in your Hierarchy window that is named "AudioSourceObject"? Likely you're thinking this name relates to your variable; sorry if that's not the case.
     
  27. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18


    https://imgur.com/mQQ6unz


    https://imgur.com/ZRCCmHQ



    dunno whey its go assigning the audio source to the variable

    Code (CSharp):
    1. using UnityEngine;
    2. using VRM;
    3.  
    4. public class VRMLipSyncMorphTarget : MonoBehaviour
    5. {
    6.     public VRMBlendShapeProxy blendShapeProxy;
    7.     public OVRLipSyncContext lipsyncContext;
    8.     GameObject AudioSourceObject;
    9.  
    10.     // smoothing amount
    11.     [Range(1, 100)]
    12.     [Tooltip("Smoothing of 1 will yield only the current predicted viseme, 100 will yield an extremely smooth viseme response.")]
    13.     public int smoothAmount = 10;
    14.  
    15.     OVRLipSync.Viseme[] visemes =
    16.     {
    17.         OVRLipSync.Viseme.aa,
    18.         OVRLipSync.Viseme.E,
    19.         OVRLipSync.Viseme.ih,
    20.         OVRLipSync.Viseme.oh,
    21.         OVRLipSync.Viseme.ou,
    22.     };
    23.  
    24.     void Start()
    25.     {
    26.         if (lipsyncContext == null)
    27.         {
    28.             Debug.LogError("VRMLipSyncMorphTarget.Start Error: " +
    29.                 "No OVRLipSyncContext component on this object!");
    30.         }
    31.         else
    32.         {
    33.             lipsyncContext.Smoothing = smoothAmount;
    34.         }
    35.     }
    36.  
    37.     void Update()
    38.     {
    39.         if(lipsyncContext == null)
    40.         {
    41.             lipsyncContext = GameObject.Find("AudioSourceObject");
    42.            
    43.             //return;
    44.         }
    45.  
    46.  
    47.         if (blendShapeProxy == null)
    48.         {
    49.             // lipsyncContext = GameObject.Find("AudioSourceObject");
    50.             blendShapeProxy = GetComponent<VRMBlendShapeProxy>();
    51.             //return;
    52.         }
    53.  
    54.         // get the current viseme frame
    55.         OVRLipSync.Frame frame = lipsyncContext.GetCurrentPhonemeFrame();
    56.         if (frame != null)
    57.         {
    58.             SetVisemeToMorphTarget(frame);
    59.         }
    60.  
    61.         // Update smoothing value
    62.         if (smoothAmount != lipsyncContext.Smoothing)
    63.         {
    64.             lipsyncContext.Smoothing = smoothAmount;
    65.         }
    66.     }
    67.  
    68.     void SetVisemeToMorphTarget(OVRLipSync.Frame frame)
    69.     {
    70.         foreach (OVRLipSync.Viseme viseme in visemes)
    71.         {
    72.             int index = (int)viseme;
    73.             switch (viseme)
    74.             {
    75.                 case OVRLipSync.Viseme.aa:
    76.                     blendShapeProxy.SetValue(BlendShapePreset.A, frame.Visemes[index]);
    77.                     break;
    78.                 case OVRLipSync.Viseme.E:
    79.                     blendShapeProxy.SetValue(BlendShapePreset.E, frame.Visemes[index]);
    80.                     break;
    81.                 case OVRLipSync.Viseme.ih:
    82.                     blendShapeProxy.SetValue(BlendShapePreset.I, frame.Visemes[index]);
    83.                     break;
    84.                 case OVRLipSync.Viseme.oh:
    85.                     blendShapeProxy.SetValue(BlendShapePreset.O, frame.Visemes[index]);
    86.                     break;
    87.                 case OVRLipSync.Viseme.ou:
    88.                     blendShapeProxy.SetValue(BlendShapePreset.U, frame.Visemes[index]);
    89.                     break;
    90.                 default:
    91.                     blendShapeProxy.SetValue(BlendShapePreset.A, 0);
    92.                     blendShapeProxy.SetValue(BlendShapePreset.I, 0);
    93.                     blendShapeProxy.SetValue(BlendShapePreset.U, 0);
    94.                     blendShapeProxy.SetValue(BlendShapePreset.E, 0);
    95.                     blendShapeProxy.SetValue(BlendShapePreset.O, 0);
    96.                     break;
    97.             }
    98.         }
    99.     }
    100. }
    101.  
     
    Last edited: Dec 13, 2021
  28. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,187
    GameObject.Find is returning a GameObject, not the component. You need to grab the component off the GameObject to assign it to the lipsyncContext variable.
     
    syb3rpunktv likes this.
  29. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,452
    I'm sorry to say but you're going around in circles here because you're NOT doing your homework in reading docs and looking at learn materials. It's been explained above several times about your basic misunderstanding here.
     
    Johan_Liebert123 likes this.
  30. syb3rpunktv

    syb3rpunktv

    Joined:
    May 8, 2014
    Posts:
    18
    thank you so much i got it i didn't know i needed the component off the audio game object as soon as you said that i was like wait its the OVRLipSyncContext of the AudioSourceObject i need not the gameobject it self

    Code (CSharp):
    1.     void Update()
    2.     {
    3.         if(lipsyncContext == null)
    4.         {
    5.             lipsyncContext = GameObject.Find("AudioSourceObject").GetComponent<OVRLipSyncContext>();
    6.          
    7.             //return;
    8.         }
    9.  
    10.  
    11.         if (blendShapeProxy == null)
    12.         {
    13.             // lipsyncContext = GameObject.Find("AudioSourceObject");
    14.             blendShapeProxy = GetComponent<VRMBlendShapeProxy>();
    15.             //return;
    16.         }
    17.  
    18.         // get the current viseme frame
    19.         OVRLipSync.Frame frame = lipsyncContext.GetCurrentPhonemeFrame();
    20.         if (frame != null)
    21.         {
    22.             SetVisemeToMorphTarget(frame);
    23.         }
    24.  
    25.         // Update smoothing value
    26.         if (smoothAmount != lipsyncContext.Smoothing)
    27.         {
    28.             lipsyncContext.Smoothing = smoothAmount;
    29.         }
    30.     }
    you are the best next time i need something like this i know i need the componts off the other object not the object itself