Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Resource Not Found Error

Discussion in 'Scripting' started by somebody1000, Aug 3, 2020.

  1. somebody1000

    somebody1000

    Joined:
    Feb 6, 2020
    Posts:
    6
    Hello, I posted a whie back about an issue I was having with presets and their parent objects. I decided to try a new approach that would not use any presets in hopes of avoiding these kinds of errors. I am now having an issue with the Resources folder that I have in that when the script I have attempts to retrieve a text resource from the folder, it cannot seem to find it. I looked up solutions, and it appears to me that I did exactly what the Unity manual explains, but it does not seem to be working for me (Here is the manual if you want to see it: https://docs.unity3d.com/Manual/LoadingResourcesatRuntime.html ). Am I missing something? Here is the code and error messages in case it is relevant.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using System.Collections.Specialized;
    5. using System.Runtime.Versioning;
    6. using System.Security.Cryptography;
    7. using UnityEngine;
    8. using UnityEngine.SceneManagement;
    9. using UnityEngine.UI;
    10.  
    11. public class QuizManager : MonoBehaviour
    12. {
    13.  
    14.     public GameObject canvas;
    15.  
    16.     GameObject CreateText(Transform canvas_transform, float x, float y, string text_to_print, int font_size, Color text_color)
    17.     {
    18.         Font arial;
    19.         GameObject UItextGO = new GameObject("question");
    20.         UItextGO.transform.SetParent(canvas_transform);
    21.  
    22.         RectTransform trans = UItextGO.AddComponent<RectTransform>();
    23.         trans.anchoredPosition = new Vector2(x, y);
    24.  
    25.         arial = (Font)Resources.GetBuiltinResource(typeof(Font), "Roboto-Bold.ttf");
    26.  
    27.         Text text = UItextGO.AddComponent<Text>();
    28.         text.font = arial;
    29.         text.text = text_to_print;
    30.         text.fontSize = font_size;
    31.         text.color = text_color;
    32.  
    33.         return UItextGO;
    34.     }  
    35.  
    36.     public void Start()
    37.     {
    38.         GameObject UItext = CreateText(canvas.transform, 100, 100, "Question 1", 24, Color.black);
    39.  
    40.        
    41.  
    42.     }
    Resource_Error.PNG Resource_Folder.PNG Resource_Error.PNG Resource_Folder.PNG
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    8,352
    I'm not sure that call will work, and I also don't think you want the
    .ttf
    in their either.

    In any case, given you put the font in Resources correctly, you could simply use this:

    Code (csharp):
    1. arial = Resources.Load<Font>("Roboto-Bold");  // don't add .ttf suffix
     
    somebody1000 likes this.
  3. somebody1000

    somebody1000

    Joined:
    Feb 6, 2020
    Posts:
    6
    This fixed it for me, Thank you!
     
    Kurt-Dekker likes this.
unityunity