Forum rules - please read before posting.

Custom Expression Script not having effect in build

Hi,

I have this Custom Action Script that allows me to choose which new portrait texture I want to change depending on which character is in scene. Here is the code:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif

    namespace AC
    {

        [System.Serializable]
        public class ChangeExpressionsAction : Action
        {

            public override ActionCategory Category { get { return ActionCategory.Custom; } }
            public override string Title { get { return "Switch Expression Textures"; } }
            public override string Description { get { return "This switches the expression textures"; } }


            public AC.Char _myCharacter;
            public int _expressionIndex;
            public Texture2D _newExpressionTexture;
            public Texture2D _newPortraitIcon;


            public override float Run()
            {
                int numExpressions = _myCharacter.expressions.Count;
                if (numExpressions == 0)
                {
                    Debug.LogError("Trying to change expression on character without expressions");
                }

                if (_newPortraitIcon != null)
                {
                    _myCharacter.portraitIcon.ReplaceTexture (_newPortraitIcon);
                }
                if (_myCharacter != null && _newExpressionTexture != null && _expressionIndex >= 0 && _expressionIndex < numExpressions)
                {
                    _myCharacter.expressions[_expressionIndex].portraitIcon.ReplaceTexture (_newExpressionTexture );
                }

                return 0f;
            }


            public override void Skip ()
            {
                Run ();
            }


            #if UNITY_EDITOR

            public override void ShowGUI ()
            {
                _newExpressionTexture = (Texture2D) EditorGUILayout.ObjectField ("New Texture:", _newExpressionTexture, typeof (Texture2D), true);
                _expressionIndex = EditorGUILayout.IntField (_expressionIndex );
                _myCharacter = (AC.Char) EditorGUILayout.ObjectField ("Character:", _myCharacter, typeof (AC.Char), true);
                _newPortraitIcon = (Texture2D) EditorGUILayout.ObjectField ("New Portrait:", _newPortraitIcon, typeof (Texture2D), true);
            }

            #endif

        }

    }

However, in my build it does not take affect, and defaults to the default portrait texture.

Any reason why this is not holding across scenes in the build?

Here are screenshots of action list, and Rufus CHaracter component:

https://www.dropbox.com/sh/ftpdgqbszvfrp89/AACrGYAPZJ_5dp13TLxW4XAqa?dl=0

Comments

  • A prefab is not the same as a runtime instance of that prefab in the scene. Your Action will affect the original Player prefab, but not the one in the scene,

    In this situation, it's best to create a dedicated "Is Player?" checkbox:

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ChangeExpressionsAction : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Custom; } }
            public override string Title { get { return "Switch Expression Textures"; } }
            public override string Description { get { return "This switches the expression textures"; } }
    
            public bool isPlayer;
            public AC.Char _myCharacter;
            public int _expressionIndex;
            public Texture2D _newExpressionTexture;
            public Texture2D _newPortraitIcon;
    
    
            public override float Run()
            {
                int numExpressions = _myCharacter.expressions.Count;
                if (numExpressions == 0)
                {
                    Debug.LogError("Trying to change expression on character without expressions");
                }
    
                if (isPlayer)
                {
                    _myCharacter = KickStarter.player;
                }
    
                if (_newPortraitIcon != null)
                {
                    _myCharacter.portraitIcon.ReplaceTexture (_newPortraitIcon);
                }
                if (_myCharacter != null && _newExpressionTexture != null && _expressionIndex >= 0 && _expressionIndex < numExpressions)
                {
                    _myCharacter.expressions[_expressionIndex].portraitIcon.ReplaceTexture (_newExpressionTexture );
                }
    
                return 0f;
            }
    
    
            public override void Skip ()
            {
                Run ();
            }
    
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                _newExpressionTexture = (Texture2D) EditorGUILayout.ObjectField ("New Texture:", _newExpressionTexture, typeof (Texture2D), true);
                _expressionIndex = EditorGUILayout.IntField (_expressionIndex );
    
                isPlayer = EditorGUILayout.Toggle ("Is Player?", isPlayer);
                if (!isPlayer)
                {
                    _myCharacter = (AC.Char) EditorGUILayout.ObjectField ("Character:", _myCharacter, typeof (AC.Char), true);
                }
                _newPortraitIcon = (Texture2D) EditorGUILayout.ObjectField ("New Portrait:", _newPortraitIcon, typeof (Texture2D), true);
            }
    
            #endif
    
        }
    
    }
    

    For a more general approach to writing Actions that connect prefab references to runtime scene instances, see this tutorial.

  • Still having issues with this script. Not working for me, see screenshots. Main portrait still not changing:

    https://www.dropbox.com/sh/nwxsusp1kouwp2u/AABC4SXC1L1yV6uaNp9uq6gNa?dl=0

    using UnityEngine;
    #if UNITY_EDITOR
    using UnityEditor;
    #endif
    
    namespace AC
    {
    
        [System.Serializable]
        public class ChangeExpressionsAction : Action
        {
    
            public override ActionCategory Category { get { return ActionCategory.Custom; } }
            public override string Title { get { return "Switch Expression Textures"; } }
            public override string Description { get { return "This switches the expression textures"; } }
    
            public bool isPlayer;
            public AC.Char _myCharacter;
            public int _expressionIndex;
            public Texture2D _newExpressionTexture;
            public Texture2D _newPortraitIcon;
    
    
            public override float Run()
            {
                int numExpressions = _myCharacter.expressions.Count;
                if (numExpressions == 0)
                {
                    Debug.LogError("Trying to change expression on character without expressions");
                }
    
                if (isPlayer)
                {
                    _myCharacter = KickStarter.player;
                }
    
                if (_newPortraitIcon != null)
                {
                    _myCharacter.portraitIcon.ReplaceTexture (_newPortraitIcon);
                }
                if (_myCharacter != null && _newExpressionTexture != null && _expressionIndex >= 0 && _expressionIndex < numExpressions)
                {
                    _myCharacter.expressions[_expressionIndex].portraitIcon.ReplaceTexture (_newExpressionTexture );
                }
    
                return 0f;
            }
    
    
            public override void Skip ()
            {
                Run ();
            }
    
    
            #if UNITY_EDITOR
    
            public override void ShowGUI ()
            {
                _newExpressionTexture = (Texture2D) EditorGUILayout.ObjectField ("New Texture:", _newExpressionTexture, typeof (Texture2D), true);
                _expressionIndex = EditorGUILayout.IntField (_expressionIndex );
    
                isPlayer = EditorGUILayout.Toggle ("Is Player?", isPlayer);
                if (!isPlayer)
                {
                    _myCharacter = (AC.Char) EditorGUILayout.ObjectField ("Character:", _myCharacter, typeof (AC.Char), true);
                }
                _newPortraitIcon = (Texture2D) EditorGUILayout.ObjectField ("New Portrait:", _newPortraitIcon, typeof (Texture2D), true);
            }
    
            #endif
    
        }
    
    }
    
  • Assign your _myCharacter variable before reading it:

    if (isPlayer)
    {
        _myCharacter = KickStarter.player;
    }
    
    int numExpressions = _myCharacter.expressions.Count;
    if (numExpressions == 0)
    {
        Debug.LogError("Trying to change expression on character without expressions");
    }
    
  • edited November 2021

    Thanks

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.