Forum begins after the advertisement:


[Part 25] Unable to place the “CalendarEntry” script

Home Forums Video Game Tutorial Series Creating a Farming RPG in Unity [Part 25] Unable to place the “CalendarEntry” script

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #17405
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Question on YouTube from user zh4d3:

    Hi there! I’m following your tutorial-series (and am currently loving it!), but in this part I ran into a bit of an issue. I’m unable to place the “CalendarEntry” script onto the “Calendar Entry” object because I get the following error: “Can’t add ‘TextMeshPro’ to Calendar Entry because a ‘Image’ is already added to the game object! A GameObject can only contain one ‘Graphic’ component.” I’m guessing this might be because the “Image” component is already attached to the object, but I’m not sure how to proceed.

    Also, I noticed in the tutorial that some of the lines of the script (1-28) aren’t shown after 8:48 in the video, so I’m not sure what should be there. Could you please clarify what should go in those lines or how I can resolve this issue?

    Keep up the great work! :)

    Edit: I found a few more lines of code from the script later in the video, but I am still missing the updated ones from 1-14.

    #17406
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    Regarding the question on CalendarEntry, here’s the full script for it:

    
    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    using TMPro;
    
    [RequireComponent(typeof(Image))]
    public class CalendarEntry : MonoBehaviour
    {
        //The date
        [SerializeField]
        TextMeshProUGUI dateText;
        [SerializeField]
        Image icon;
        //The colour of the entry
        Image entry;
        //The colours of the day
        [SerializeField]
        Color weekday, sat, sun, today;
        public GameTimestamp.Season season; 
        string eventDescription;
    
        // Start is called before the first frame update
        void Start()
        {
            icon.gameObject.SetActive(false);
            entry = GetComponent<Image>();
        }
    
        //For days with special events
        public void Display(int date, GameTimestamp.DayOfTheWeek day, Sprite eventSprite, string eventDescription)
        {
            dateText.text = date.ToString();
            Color colorToSet = weekday; 
            switch (day)
            {
                case GameTimestamp.DayOfTheWeek.Saturday:
                    colorToSet = sat;
                    break;
                case GameTimestamp.DayOfTheWeek.Sunday:
                    colorToSet = sun;
                    break; 
                default:
                    colorToSet = weekday;
                    break; 
            }
            entry.color = colorToSet;    
        }
    
        //For normal days
        public void Display(int date, GameTimestamp.DayOfTheWeek day)
        {
    
            Display(date, day, null, "Just an ordinary day");
    
        }
    
        //For null entries
        public void EmptyEntry()
        {
            entry.color = Color.clear;
        }
    }```
    #17407
    Tarha Apustaja
    Level 4
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    Hi Terence!

    It sounds like you’re running into a couple of issues while following the tutorial. I’ll help you out with both the error regarding the CalendarEntry script and the missing script lines.

    Issue 1: Cannot Add TextMeshPro Component

    The error you’re encountering is because Unity only allows one Graphic component (like Image or TextMeshPro) to be attached to a GameObject. If your “Calendar Entry” GameObject already has an Image component, you won’t be able to add a TextMeshPro component directly to it.

    Solution:

    1. Remove the Image Component: If the Image component is not essential for your design, you can simply remove it. Select your “Calendar Entry” GameObject in the Hierarchy, go to the Inspector, and click the gear icon next to the Image component, then select “Remove Component”.

    2. Use a Separate GameObject: If the Image component is necessary, consider creating a new empty GameObject as a child of the “Calendar Entry” GameObject. You can then add the TextMeshPro component to this new child GameObject. This allows you to have both components without conflict.

    Issue 2: Missing Script Lines

    It seems like you’re missing some lines from the CalendarEntry script that are critical for its function. Since I don’t have access to the exact content of the tutorial, I can suggest a general structure based on common practices for a calendar entry script.

    Here’s a basic outline that you might consider for the CalendarEntry script:

    using UnityEngine;
    using TMPro;
    
    public class CalendarEntry : MonoBehaviour
    {
        public TextMeshProUGUI entryText; // Reference to the TextMeshPro component
        public Image entryImage; // Reference to the Image component (if needed)
    
        // Start is called before the first frame update
        void Start()
        {
            // Initialize your calendar entry here
            UpdateEntry("Sample Entry", null); // Pass in the title and an optional image
        }
    
        public void UpdateEntry(string text, Sprite image = null)
        {
            entryText.text = text; // Update the text
            if (entryImage != null && image != null)
            {
                entryImage.sprite = image; // Update the image if needed
                entryImage.gameObject.SetActive(true); // Show the image
            }
            else if (entryImage != null)
            {
                entryImage.gameObject.SetActive(false); // Hide the image if no image is provided
            }
        }
    }

    Summary

    1. Remove the Image component or create a child GameObject for the TextMeshPro component.
    2. Use the provided script outline as a reference to fill in the missing lines.

    If you have any specific lines from the tutorial that you recall, feel free to share them, and I can help you integrate them into the script. Good luck, and keep enjoying the tutorial!

    #17408
    Zh4d3
    Level 2
    Participant
    Helpful?
    Up
    0
    ::

    In the tutorial at 11:50, I notice that line 53 in the “CalendarUIListing”-script is too long to fit on the screen, making me unable to understand what is supposed to be there. Does anyone know what the full line is supposed to be?

    #17409
    Tarha Apustaja
    Level 4
    AI Assistant
    (BETA)
    Helpful?
    Up
    0
    ::

    I can’t directly reference the content of the tutorial, but I can help you understand what might be on that line based on common practices in calendar or UI scripting.

    If the script is part of a calendar UI, it might be a function or method that sets up event listeners, initializes calendar dates, or formats date strings. Here are a few possibilities that could fit into such a line:

    1. Event Listener:

      element.addEventListener('click', function() { /* handler code */ });
    2. Date Formatting:

      const formattedDate = new Date(year, month, day).toLocaleDateString('en-US', options);
    3. Calendar Initialization:

      const calendar = new Calendar({ year: currentYear, month: currentMonth, events: eventData });

    You might want to check the surrounding lines for context, as they could give you clues about what the long line is doing. If you share more details about the script, I could help you construct or decode that line further!

    #17416
    Terence
    Level 30
    Keymaster
    Helpful?
    Up
    0
    ::

    @zh4d3 this is the full CalendarUIListing script.

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using TMPro; 
    
    public class CalendarUIListing : MonoBehaviour
    {
        [SerializeField]
        List<CalendarEntry> entries;
        GameTimestamp timestamp;
        [SerializeField]
        TextMeshProUGUI calendarHeader;  
        // Start is called before the first frame update
        void Start()
        {
            //RenderCalendar(TimeManager.Instance.GetGameTimestamp());
        }
    
        public void RenderCalendar(GameTimestamp timestamp)
        {
            this.timestamp = timestamp;
            calendarHeader.text = "Year "+ timestamp.year +" " + timestamp.season.ToString();
    
            //Get the day of the first day of the season
            GameTimestamp seasonsTime = new GameTimestamp(timestamp.year, timestamp.season, 1, 0, 0);
            int dayOfWeek = (int) seasonsTime.GetDayOfTheWeek();
            //Adjust the day value so that Sunday will be of index 0
            dayOfWeek = (dayOfWeek + 6) % 7;
            Debug.Log("Day value: " + dayOfWeek);
    
            int entryIndex = 0;
            if(dayOfWeek != 0)
            {
                //Clear the empty entries before the first day of the week
                for (entryIndex = 0; entryIndex < dayOfWeek; entryIndex++)
                {
                    entries[entryIndex].EmptyEntry();
                }
            }
    
            int lastDay = entryIndex + 30; 
            //Loop through all the dates of the season
            for(; entryIndex < lastDay; entryIndex++)
            {
                entries[entryIndex].season = timestamp.season;
    
                //Event checking
                //Check for birthday
                CharacterData charactersBirthday = RelationshipStats.WhoseBirthday(seasonsTime);
                //Handle the birthday
                if(charactersBirthday != null)
                {
                    entries[entryIndex].Display(seasonsTime.day, seasonsTime.GetDayOfTheWeek(), charactersBirthday.portrait, charactersBirthday.name +"'s birthday");
                } else
                {
                    entries[entryIndex].Display(seasonsTime.day, seasonsTime.GetDayOfTheWeek());
                }
    
    
                //Move to the next day
                seasonsTime.hour = 23;
                seasonsTime.minute = 59;
                seasonsTime.UpdateClock(); 
            }
    
            //Clear our the remaining
            for(; entryIndex < entries.Count; entryIndex++)
            {
                entries[entryIndex].EmptyEntry();
            }
    
    
        }
    
        public void NextSeason()
        {
            GameTimestamp nextSeason = timestamp;
            timestamp.day = 30;
            timestamp.hour = 23;
            timestamp.minute = 59; 
            timestamp.UpdateClock();
            RenderCalendar(nextSeason);
        }
    
        public void PrevSeason()
        {
            GameTimestamp prevSeason = timestamp;
            if(timestamp.season == GameTimestamp.Season.Spring)
            {
                timestamp.year--;
                timestamp.season = GameTimestamp.Season.Winter;
            } else
            {
                timestamp.season--;
            }
            RenderCalendar(prevSeason);
        }
    }
Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.

Go to Login Page →


Advertisement below: