HELP PLS Making object button that plays video

Hey! So I am trying to recreate the classic tamagotchi game on unity. I have created a 3d model of the tamagotchi device and I want the game to play on the screen of the device.

My plan was to have the video player attached to the a box object that acts as a game screen. I have little buttons on the device made out of cylinders. I have created a video of the initial egg hatching animation and I want it to play when the middle button is pressed as a way of starting the game. I have put a collider on this button.

I have literally NO UNITY OR CODING experience. This is for a class I am taking and the professor is not the best at explaining things and we did not look at these specific codes in class.

I put a collider on the cylinder object so I can ray cast on it and potentially put a onmousedown command thing so that when the object is clicked an event can occur. The event I want to happen is a video playing within a box like a tv screen.

This is what I have so far and of course it doesnt even run. But i do think some of what I have is on the right track.


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

public class startbutton : MonoBehaviour
{
public GameObject startbutton;
public UnityEvent OnMouseDown= new UnityEvent();

// Start is called before the first frame update
void Start()
{
startbutton = Cyllinder002.gameObject;

GameObject box1 = GameObject.Find("Box001");

var videoPlayer = box1.AddComponent<UnityEngine.Video.VideoPlayer>();

videoPlayer.playOnAwake = false;

videoPlayer.url = "/Users/jponcel/Desktop/unity/New Unity Project/Assets/tamahatchvid.mp4";

videoPlayer.isLooping = false;

}

// Update is called once per frame
void Update()
{
var Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit Hit;

if (Input.GetMouseButtonDown(0))
{
if (Physics.Raycast(Ray, out Hit) && Hit.collider.gameobject == gameObject)
{
Debug.Log("Buttton Clicked")
void OnMouseDown() {

public VideoPlayer videoPlayer;
public void playVideo();

videoPlayer = GetComponent<VideoPlayer>();
videoPlayer.Play();

}

}
}
}
}

lmk if u have any ideas of how i can execute this properly i am close 2 sobbing
thank u sm



Steps to success:

  1. get some code and a scene set up that senses clicking on the object in question. Isolate that, get it 100% working, testing via Debug.Log() (see below)

  2. make a script that plays a video by whatever means you choose (start with the docs or tutorials)

  3. Connect the final result of step #1 above to step #2

To debug what you have going now, try this approach:

What is often happening in these cases is one of the following:

  • the code you think is executing is not actually executing at all
  • the code is executing far EARLIER or LATER than you think
  • the code is executing far LESS OFTEN than you think
  • the code is executing far MORE OFTEN than you think
  • the code is executing on another GameObject than you think it is
  • you’re getting an error or warning and you haven’t noticed it in the console window

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?
  • are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)

Knowing this information will help you reason about the behavior you are seeing.

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target, such as this answer or iOS: How To - Capturing Device Logs on iOS or this answer for Android: How To - Capturing Device Logs on Android

Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

You must find a way to get the information you need in order to reason about what the problem is.

@julesnpl What class is this for? The instructor is asking the students to code in C# with no coding experience?

Maybe something like this? You would have to name the gameobjects appropriately, “LeftButton”, “MiddleButton”, etc.

if (Input.GetMouseButtonDown(0))
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    {
        switch(hit.collider.name)
        {
            case "LeftButton":
                Debug.Log("Left button pressed");
                break;

            case "MiddleButton":
                Debug.Log("Middle button pressed");
                break;

            case "RightButton":
                Debug.Log("Right button pressed");
                break;
        }
    }
}

ya kms its for 3d modelling lol they rlly catfished me

1 Like

thank u but could u explain what all of this means a lil more bc i have no idea pls

// Check if the primary button (often left button) was pressed.
if (Input.GetMouseButtonDown(0))
{
    // Return a ray going from camera through a screen point. In this case,
    // the screen point in where the mouse click happened.
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

    // Cast a ray of infinite depth. Did we hit something? If we did, output
    // that information to hit so we can use it later.
    if (Physics.Raycast(ray, out hit, Mathf.Infinity))
    {
        // Do something based on the name of the gameobject the raycast hit.
        switch(hit.collider.name)
        {
            // If the object we hit is called "LeftButton" do this...
            case "LeftButton":
                // Log a message in the console.
                Debug.Log("Left button pressed");
                // escape the switch.
                break;

            // If the object we hit is called "MiddleButton" do this...
            case "MiddleButton":
                // Log a message in the console.
                Debug.Log("Middle button pressed");
                // escape the switch.
                break;

            // If the object we hit is called "RightButton" do this...
            case "RightButton":
                // Log a message in the console.
                Debug.Log("Right button pressed");
                // escape the switch.
                break;
        }
    }
}