Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Problem on a 3d tutorial

Discussion in 'General Discussion' started by CatsInThePot, Mar 7, 2023.

  1. CatsInThePot

    CatsInThePot

    Joined:
    Sep 4, 2020
    Posts:
    5
    Excuse my english but i'm using googletranslate.
    I'm studying tutorials but I have a big problem with a script that serves to get information about an object watched by the character. link

    In practice, once the script has been inserted, the Interaction_Info_UI option should appear in the inspector window as in the video, but it does not appear (https://image.forumfree.it/1/3/9/4/9/5/8/7/1677847535.jpg) .
    I've posted the script, if anyone could help me:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;

    public class SelectionManager : MonoBehaviour
    {

    public GameObject interaction_Info_UI;
    Text interaction_text;
    private void Start()
    {
    interaction_text = interaction_Info_UI.GetComponent<text>();
    }
    void Update()
    {
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, out hit))
    {
    var selectionTransform = hit.transform;
    if (selectionTransform.GetComponent<interactableobject>())
    {
    interaction_text.text = selectionTransform.GetComponent<interactableobject>().GetItemName();
    interaction_Info_UI.SetActive(true);
    }
    else
    {
    interaction_Info_UI.SetActive(false);
    }
    }
    }
    }
     
  2. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    570
    Go to "Code" in the WYSIWYG editor and insert your code to format it.

    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UI;
    6. public class SelectionManager: MonoBehaviour {
    7.   public GameObject interaction_Info_UI;
    8.   Text interaction_text;
    9.   private void Start() {
    10.     interaction_text = interaction_Info_UI.GetComponent < text > ();
    11.   }
    12.   void Update() {
    13.     Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    14.     RaycastHit hit;
    15.     if (Physics.Raycast(ray, out hit)) {
    16.       var selectionTransform = hit.transform;
    17.       if (selectionTransform.GetComponent < interactableobject > ()) {
    18.         interaction_text.text = selectionTransform.GetComponent < interactableobject > ().GetItemName();
    19.         interaction_Info_UI.SetActive(true);
    20.       } else {
    21.         interaction_Info_UI.SetActive(false);
    22.       }
    23.     }
    24.   }
    25. }
    26.  
    The url you posted shows this:

    Forbidden
    You don't have permission to access this resource.

    ---------------------

    And, the script you've given will not compile. So it is hard to say what the problem is with it. For example "interaction_text = interaction_Info_UI.GetComponent < text > ();" where "< text >" must be "< Text >" to compile.

    There are other issues like that, which I assume happened when copying the code at some point. But most likely that variable is not appearing in the editor because your code is not compiling. Look at the Console window, and find the errors listed (if any).
     
    Last edited: Mar 7, 2023
  3. CatsInThePot

    CatsInThePot

    Joined:
    Sep 4, 2020
    Posts:
    5
  4. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    570
    So that is the problem. InteractableObject.cs doesn't exist. You need to create it. or the class name in that file doesn't match "InteractableObject".
     
  5. CatsInThePot

    CatsInThePot

    Joined:
    Sep 4, 2020
    Posts:
    5
    I understand....but this is how it works for him in the tutorial, the script is written the same and the objects have the name he wrote. Did he omit any steps?
     
  6. BABIA_GameStudio

    BABIA_GameStudio

    Joined:
    Mar 31, 2020
    Posts:
    491
    He had already created that script, but didn't show it being done in the video.
    Look at the whole video. At around the 14min time he shows the contents of the InteractableObject script, so you should be able to create the script from what he shows.
     
  7. CatsInThePot

    CatsInThePot

    Joined:
    Sep 4, 2020
    Posts:
    5
    ... seen now.... is that it should be a step by step tutorial and I didn't imagine that things would be explained later -.-' Thank you :)
     
  8. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    570
    It seems you are very new to programming. I recommend starting with a tutorial on the basics of programming. Before you start making a game, you should be able to see errors in the console, and understand what this particular error means. If you understand the basics on debugging and syntax, I think you will have a better time following this and other tutorials. As it stands, needing to come to the forum for this particular issue is a sign that you need to learn more about the basics of programming - because this is one of the simplest and easiest errors you will encounter.

    (I don't mean that in an insulting way. We all start from the beginning, and I just want to direct you down a path that will be less frustrating for you/be a better starting experience.)
     
  9. CatsInThePot

    CatsInThePot

    Joined:
    Sep 4, 2020
    Posts:
    5
    I can't blame you, I actually studied c++ (standard programming), I thought I could understand c# by unity, but I think it will be difficult to understand :)
     
  10. tsibiski

    tsibiski

    Joined:
    Jul 11, 2016
    Posts:
    570
    You'll find it is definitely different, but I suspect that after you get over the initial hurdles and confusion of the changes, you might find it is far easier. The syntax, in my opinion, is much more human readable. And you don't have to ever worry about garbage collection. I found C# to be 100x easier than C++.