Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem reading playmaker FSM objects and obtaining mesh reference from them in C#

Discussion in 'Scripting' started by terrygreer, Jun 11, 2015.

  1. terrygreer

    terrygreer

    Joined:
    Sep 27, 2014
    Posts:
    18
    I'm using playmakers global variables - and I have a number of mesh objects that I reference using these global variables.

    I now need to access those and get the mesh information into c#
    I can access thye global variables easily enough - e.g.:

    Object temp = PlayMakerGlobals.Instance.Variables.GetFsmObject("Mesh_Classic_Pawn").Value;

    which gives me a reference to the object - but how do I get the mesh reference form this?

    I've spent over 2 hours trying to find reference for this but the only thing I can find is MeshFilter - but I can't get that to work - and I can't seem to find out how to point it at that specific object.

    If you have any idea - I'd be very grateful
     
  2. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Mesh is an Object type. In the Playmaker Object Variable are you choosing UnityEngine>Mesh (or something like that)?

    The .Value field returns the actual variable so you should be able to store them as a Mesh and that is what .Value will be.
     
  3. terrygreer

    terrygreer

    Joined:
    Sep 27, 2014
    Posts:
    18
    Thanks for replying.
    To assign the playmaker global variable I'm dragging the mesh from the object in the asset folder (see image below) direct into the variable. The variable is an Object variable - there isn't one specifically upload_2015-6-11_14-26-28.png for meshes that i know of. This works fine in the sections where I use Playmaker.
    But trying to assign that to a mesh variable in c# is where I'm having problems.
    If I assume it is already a mesh and try and assign it directly - e.g:
    Public Mesh Pawn;
    Pawn = PlayMakerGlobals.Instance.Variables.GetFsmObject("Mesh_Classic_Pawn").Value;

    I get an error : Cannot implicitly convert type 'UnityEngine.Object' to 'UnityEngine.Mesh'. An explicit conversion exists (are you missing a cast?)

    I can't figure out how to convert it.
    upload_2015-6-11_14-26-28.png
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    You're assigning the Mesh as a generic Object type so when you create a Mesh variable and want to fill it with a generic Object type you have to cast it as the correct type.

    So you'll do something like...

    Code (csharp):
    1.  
    2. Pawn = PlayMakerGlobals.Instance.Variables.GetFsmObject("Mesh_Classic_Pawn").Value as Mesh;
    3.  
    4. or
    5.  
    6. Pawn = (Mesh)PlayMakerGlobals.Instance.Variables.GetFsmObject("Mesh_Classic_Pawn").Value;
    7.  
    Something like that.... The error is quite literal in this case. You can't make an Object type magically become a Mesh type without telling it to do that explicitly.
     
  5. terrygreer

    terrygreer

    Joined:
    Sep 27, 2014
    Posts:
    18
    Thank you - that is an enormous help.

    I thought I'd tried the first of those and got an error message - but it seems to work now. I must have made a mistake and then discounted that approach.
    Thanks for clearing it up.

    The second approach I would never have thought of trying (I didn't know the syntax could work that) - still a novice at this coding thing - but I am improving.

    Very much appreciated and thanks again.