Hello,
I use a VideoPlayer GameObject to display different videos depending on where the player presses. Works like a charm.
the buttons send the corresponding video number and make the video window active (picture 1+3)
in the VideoPlayer the videos are all registered according to their number (picture 2)
in the canvas, they are simple circles that you press. Pressing on the video makes the video object visibility inactive.
But I run into a problem, either in my logical thinking or in the script: Although all buttons are just duplicates from the same, 2 of them go wrong and show the same video. I checked the numbers! If I swap the objects in the hierarchy, it works.
But what does the hierarchy have to do with exactly these two objects? As the script is AI generated, I’d rather think, there is a mistake, there is a better way to code this for sure, but I am not professional enough, I wish I would be.
Cheers
This is the script:
using UnityEngine;
using UnityEngine.Video;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
public class VideoControllerAndListV2 : MonoBehaviour
{
public VideoPlayer videoPlayer;
public RawImage displayImage;
public RenderTexture renderTexture;
// Liste fĂĽr Videos anstelle einzelner Variablen
public List<VideoClip> videos = new List<VideoClip>();
private void Start()
{
// Stelle sicher, dass der VideoPlayer richtig konfiguriert ist
if (videoPlayer == null)
videoPlayer = GetComponent<VideoPlayer>();
// Verbinde VideoPlayer mit displayImage, wenn beides vorhanden ist
if (videoPlayer != null && displayImage != null && renderTexture != null)
{
videoPlayer.targetTexture = renderTexture;
displayImage.texture = renderTexture;
}
// Stoppe eventuell automatisch startende Videos
videoPlayer.Stop();
}
// Allgemeine Methode zum Abspielen eines Videos per Index
public void PlayVideoByIndex(int index)
{
if (index >= 0 && index < videos.Count && videos[index] != null)
{
PlayVideo(videos[index]);
}
else
{
Debug.LogWarning("Video-Index nicht gĂĽltig oder Video ist null: " + index);
}
}
private void PlayVideo(VideoClip clip)
{
// Stoppe das aktuelle Video
videoPlayer.Stop();
// Setze den neuen Clip
videoPlayer.clip = clip;
// Warte einen Frame und starte dann das Video
// Dies verhindert Flackern beim Wechsel
StartCoroutine(PlayAfterFrame());
}
private IEnumerator PlayAfterFrame()
{
yield return null; // Warte einen Frame
videoPlayer.Play();
}
// Zusätzliche Kontrollmethoden
public void StopVideo()
{
if (videoPlayer != null)
videoPlayer.Stop();
}
public void PauseVideo()
{
if (videoPlayer != null)
videoPlayer.Pause();
}
}
Sounds like you wrote a bug… and that means… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.
When something like this happens it’s usually some sort of mixup.
Print the things you are using, like the video asset and the game object involved. Be sure to include their GetInstanceID() in the log. Then put the Inspector in Debug mode to see the object/asset IDs so you can verify that these don’t just happen to have the same name but are in fact the expected objects (or not) by comparing their IDs.
Comments like these are pretty useless and only make the code harder to read because prose intersects with code without ADDING any additional information that’s not already immediately obvious from looking at the code.
Just based on the wording I get the feeling those are AI generated comments? Since it’s odd to comment “ensure object is configured” to assigning an object reference.
Also by looking at this I guess your problem might be the renderTexture being null. Since that would not make the assignment and thus the previous video may be carried over.
Hi @Kurt-Dekker and @CodeSmile
thank you so much for helping me out! I 100% appreciate your answers.
I will dive into this soon. Yes, as I wrote in the beginning, the script is AI generated. I tested it against another AI to make sure to get at least a “2nd opinion”
That is excellent, but neither of those things is a substitute for debugging.
I see AI get it completely wrong in at least one part of the code 100% of the time. I’m not alone in this observation. Remember that unlike most other human endeavors, software generally has to be 100% correct or it will fail to work.
When you are ready to learn, these are the only two steps you need:
Two steps to tutorials and / or example code:
do them perfectly, to the letter (zero typos, including punctuation and capitalization)
stop and understand each step to understand what is going on.
If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.
I agree with you 100%. I still make whole app content here and am grateful that AI gives me the opportunity here without having to pay another bill to the coder (which I love to do, but just don’t have enough support/ money). I’m doing a big art project and I’m curious as a child how people will like it. I will hopefully learn to code better while I am on it. I made huge progress already. I want to understand the code, kind of like learning a foreign language.