Skip to content
The Chat Archive
mistral-medium· Mistral AI Arena Human Preference 55K

In unity, i have 4 pillars. Each contains 4 symbols sprite renderer. In the PlaceSymbol,…

  1. User

    In unity, i have 4 pillars. Each contains 4 symbols sprite renderer. In the PlaceSymbol, function i want to change the shader colours of each symbol sprite renderer when the player place a symbol in the correct order.

    The pillar hiearchy structure goes like this : LovecraftianPuzzle -Columns --Columns_square ---Symbols ----SpriteRenderer ----SpriteRenderer ----SpriteRenderer ----SpriteRenderer --Columns_square ---Symbols ----SpriteRenderer ----SpriteRenderer ----SpriteRenderer ----SpriteRenderer

    The Script : using System.Collections; using System.Collections.Generic; using UnityEngine;

    public class LovecraftianPuzzle : MonoBehaviour { public enum EldritchSymbols { Nyarlathotep, Azathoth, Cthulhu, ShubNiggurath, YogSothoth } public Transform[] symbolSlots; public GameObject[] eldritchSymbolPrefabs; public List books; public GameObject portal; public Animator portalAnimator; public AudioSource gatewayAudio; public AudioClip placeSymbolSound; public AudioClip puzzleSolvedSound; public GameObject bookParticlePrefab; public GameObject shadowPeoplePrefab; private GameObject player; private AudioSource playerAudioSource; private InventoryManager InvManager; private AudioSource puzzleAudio; private FearSystem fearSystem;

    private void Start() { player = GameObject.Find("Player"); playerAudioSource = player.GetComponent(); puzzleAudio = GetComponent(); InvManager = FindAnyObjectByType(typeof(InventoryManager)) as InventoryManager; fearSystem = FindAnyObjectByType(typeof(FearSystem)) as FearSystem; books = new List { new Book { title = "Invocations of The Cosmic Horror", symbolType = EldritchSymbols.Nyarlathotep, symbolPrefab = eldritchSymbolPrefabs[0] }, new Book { title = "Mark Of The Old Gods", symbolType = EldritchSymbols.Azathoth, symbolPrefab = eldritchSymbolPrefabs[1] }, new Book { title = "The House Of R'lyeh", symbolType = EldritchSymbols.Cthulhu, symbolPrefab = eldritchSymbolPrefabs[2] }, new Book { title = "The Shadow People", symbolType = EldritchSymbols.ShubNiggurath, symbolPrefab = eldritchSymbolPrefabs[3] }, new Book { title = "The Unknown and The Deep Void", symbolType = EldritchSymbols.YogSothoth, symbolPrefab = eldritchSymbolPrefabs[4] } }; } private List correctOrder = new List() { EldritchSymbols.Nyarlathotep, EldritchSymbols.Azathoth, EldritchSymbols.Cthulhu, EldritchSymbols.ShubNiggurath, EldritchSymbols.YogSothoth };

    private List currentOrder = new List();

    private Dictionary<EldritchSymbols, int> symbolToSlotIndex = new Dictionary<EldritchSymbols, int>() { { EldritchSymbols.Nyarlathotep, 0 }, { EldritchSymbols.Azathoth, 1 }, { EldritchSymbols.Cthulhu, 2 }, { EldritchSymbols.ShubNiggurath, 3 }, { EldritchSymbols.YogSothoth, 4 } }; [System.Serializable] public class Book { public string title; public EldritchSymbols symbolType; public GameObject symbolPrefab; } public void PlaceSymbol(EldritchSymbols symbol) { int bookIndex = books.FindIndex(b => b.symbolType == symbol); Book book = books.Find(b => b.symbolType == symbol); string title = book?.title;

    // Check if the book index is valid and the corresponding slot is empty
    if (bookIndex >= 0 && bookIndex < books.Count && symbolSlots[bookIndex].childCount == 0)
    {
        bool itemFound = false; // Flag variable to check if the item was found in the inventory
    
        for (int t = 0; t < InvManager.MaxItems; t++) //Starting a loop in the slots of the inventory:
        {
            if (InvManager.Slots[t].IsTaken == true) //Checking if there's an item in this slot.
            {
                Item ItemScript = InvManager.Slots[t].Item.GetComponent<Item>(); //Getting the item script from the items inside the bag.
                if (ItemScript.Name == title)
                {
                    itemFound = true; // Set the flag to true
                    ItemScript.ItemDropType = Item.ItemDropTypes.Destroy;
                    InvManager.RemoveItem(ItemScript.gameObject.transform, 1);
                    break; // Break out of the loop since the item was found
                }
            }
        }
    
        if (itemFound) // Check if the item was found in the inventory
        {
            GameObject symbolObject = Instantiate(
                books[bookIndex].symbolPrefab,
                symbolSlots[bookIndex].position,
                Quaternion.Euler(0, 24, -90), // Rotate 90 degrees around the Z axis
                symbolSlots[bookIndex]);
    
            Item itemScript = symbolObject.GetComponent<Item>();
            itemScript.name = itemScript.Name;
    
            if (!currentOrder.Contains(symbol)) // Check if the symbol is already in the list
            {
                currentOrder.Add(symbol); // Add it only if it's not present
            }
            playerAudioSource.PlayOneShot(placeSymbolSound);
        }
    
        else
        {
            NotificationUI notificationUI = FindObjectOfType(typeof(NotificationUI)) as NotificationUI;
            notificationUI.SendMsg("The book " + books[bookIndex].title + " is missing");
        }
    }
    NarrativeUI narrative = FindObjectOfType(typeof(NarrativeUI)) as NarrativeUI;
    narrative.SendMsg("Symbol of " + books[bookIndex].symbolType, 4);
    StartCoroutine(player.GetComponent<PlayerInteraction>().InteractionDelay());
    CheckPuzzleSolved();
    

    }

    private void CheckPuzzleSolved() { if (currentOrder.Count == correctOrder.Count) { bool isSolved = true;

        for (int i = 0; i < correctOrder.Count; i++)
        {
            if (currentOrder[i] != correctOrder[i])
            {
                isSolved = false;
                break;
            }
        }
    
        if (isSolved)
        {
            StartCoroutine(GatewayAnimation());
        }
    }
    

    } public IEnumerator GatewayAnimation() { portalAnimator.SetBool("OpenGateway", true); MeshCollider gatewayMeshFilter = portalAnimator.gameObject.GetComponent(); gatewayAudio.Play(); puzzleAudio.PlayOneShot(puzzleSolvedSound); gatewayMeshFilter.enabled = false; yield return new WaitForSeconds(1f); portalAnimator.SetBool("OpenGateway", false);

    foreach (Transform slot in symbolSlots)
    {
        if (slot.childCount > 0)
        {        
            Vector3 effectPosition = slot.position; // Modify as needed to align with the symbol
            Instantiate(bookParticlePrefab, effectPosition, Quaternion.identity, slot);
            GameObject shadowPeople = Instantiate(shadowPeoplePrefab, effectPosition, Quaternion.identity, slot);
            shadowPeople.GetComponent<AudioSource>().enabled = false;
            shadowPeople.name = shadowPeoplePrefab.name;
            fearSystem.currentFear += 100;
    
            Destroy(slot.GetChild(1).gameObject, 5);
    
        }
    }
    

    }

    public void ResetPuzzle() { currentOrder.Clear();

    foreach (Transform slot in symbolSlots)
    {
        if (slot.childCount > 0)
        {
            Destroy(slot.GetChild(0).gameObject);
        }
    }
    

    }

    public void RemoveSymbol(EldritchSymbols symbol) { // Find the index of the symbol in the currentOrder list int index = currentOrder.FindIndex(s => s == symbol);

    int bookIndex = books.FindIndex(b => b.symbolType == symbol);
    Book book = books.Find(b => b.symbolType == symbol);
    string title = book?.title;
    
    // Check that the index is valid
    if (index >= 0 && index < currentOrder.Count)
    {
        // Find the corresponding symbol in the symbolSlots array
        for (int i = 0; i < symbolSlots.Length; i++)
        {
            if (symbolSlots[i].childCount > 0)
            {
                GameObject symbolObject = symbolSlots[i].GetChild(0).gameObject;
                EldritchSymbols symbolInSlot = symbolObject.GetComponent<EldritchSymbol>().symbolType;
    
                if (symbolInSlot == symbol)
                {
                    // Remove the symbol from currentOrder and destroy the symbol object
                    currentOrder.RemoveAt(index);
                    playerAudioSource.PlayOneShot(placeSymbolSound);
    
                    // Instantiate a new instance of the book prefab and add it to inventory
                    GameObject bookPrefab = books.Find(b => b.symbolType == symbol).symbolPrefab;
    
                    // Instantiate an item from the Item prefab
                    GameObject bookInstantiate = Instantiate(bookPrefab);
                    Item bookItemScript = bookInstantiate.GetComponent<Item>();
                    EldritchSymbols bookSymbol = bookInstantiate.GetComponent<EldritchSymbol>().symbolType;
                    bookItemScript.name = bookItemScript.Name;
                    bookSymbol = symbolInSlot;
                    InvManager.AddItem(bookInstantiate.transform);
                    //InvManager.AddItem("AddItem(Transform item). How To Add The Book That Match The Symbol In Inventory?");
                    Destroy(symbolObject);
                    StartCoroutine(player.GetComponent<PlayerInteraction>().InteractionDelay());
                    break;
                }
            }
        }
    }
    

    } }

  2. Assistant

    To change the shader colors of each symbol sprite renderer, you can add a new method called ChangeSymbolColor in your LovecraftianPuzzle script. This method will take a Color parameter and the index of the symbol you want to change the color for.

    Here's an example of how the ChangeSymbolColor method can be implemented:

    public void ChangeSymbolColor(Color color, int symbolIndex)
    {
        // Find the Columns_square game object that contains the symbol you want to change the color for
        Transform columnsSquare = transform.Find("Columns").Find("Columns_square");
        if (columnsSquare != null && symbolIndex >= 0 && symbolIndex < columnsSquare.childCount)
        {
            // Find the Symbols game object that contains the sprite renderers
            Transform symbols = columnsSquare.GetChild(symbolIndex).Find("Symbols");
            if (symbols != null)
            {
                // Loop through all the sprite renderers in the Symbols game object and change their color
                foreach (SpriteRenderer spriteRenderer in symbols.GetComponentsInChildren<SpriteRenderer>())
                {
                    spriteRenderer.color = color;
                }
            }
        }
    }
    

    You can then call this method in your PlaceSymbol method to change the color of the symbol sprite renderers when the player places a symbol in the correct order. Here's an example of how you can call the ChangeSymbolColor method:

    private void CheckPuzzleSolved()
    {
        if (currentOrder.Count == correctOrder.Count)
        {
            bool isSolved = true;
    
            for (int i = 0; i < correctOrder.Count; i++)
            {
                if (currentOrder[i] != correctOrder[i])
                {
                    isSolved = false;
                    break;
                }
            }
    
            if (isSolved)
            {
                // Change the color of the symbol sprite renderers to green
                for (int i = 0; i < correctOrder.Count; i++)
                {
                    ChangeSymbolColor(Color.green, i);
                }
    
                StartCoroutine(GatewayAnimation());
            }
        }
    }
    

    In this example, the ChangeSymbolColor method is called for each symbol in the correct order, passing in the green color to change the sprite renderers to.

    I hope this helps! Let me know if you have any questions.

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.