Skip to main content
added 303 characters in body
Source Link

I'm making a 2D game using Unity and I want to implement cutscenes that change over at certain lines.

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

public class CgChanger : MonoBehaviour
{
   public Image imageToChange; // Reference to the UI Image component
    public Sprite[] sprites; // Array of sprites to cycle through`

    private int currentSpriteIndex = 0;

    void Start()
    {
        if (imageToChange == null || sprites == null || sprites.Length < 6)
        {
            Debug.LogError("ImageToChange or Sprites are not assigned or Sprites array has fewer than 6 elements.");
            return;
        }

        // Initialize with the first sprite
        imageToChange.sprite = sprites[currentSpriteIndex];
    }

    void Update()
    {
        // Check if the left mouse button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            // Move to the next sprite in the array
            currentSpriteIndex++;

            // Ensure we do not exceed the array bounds
            if (currentSpriteIndex >= sprites.Length)
            {
                currentSpriteIndex = 0; // Reset to the first sprite if the end is reached
            }

            // Change the image sprite to the next one in the array
            imageToChange.sprite = sprites[currentSpriteIndex];
        }
    }
}

This is the code I have currently, but it's flawed and I'm not sure how to fix it. I don't want it to change when the person clicks down on the mouse like it is with the text, and how it is with the images currently. That messes up the pacing if someone clicks more than once per line. How would I go about changing this?

Edit: I realize I wasn't as clear as I should have been with what I want. i have a certain amount of cutscene images that I want to stay on screen while certain lines are displayed, before switching to another image. If you've ever seen the cutscenes from the game Danganronpa, it's a bit like that

I'm making a 2D game using Unity and I want to implement cutscenes that change over at certain lines.

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

public class CgChanger : MonoBehaviour
{
   public Image imageToChange; // Reference to the UI Image component
    public Sprite[] sprites; // Array of sprites to cycle through`

    private int currentSpriteIndex = 0;

    void Start()
    {
        if (imageToChange == null || sprites == null || sprites.Length < 6)
        {
            Debug.LogError("ImageToChange or Sprites are not assigned or Sprites array has fewer than 6 elements.");
            return;
        }

        // Initialize with the first sprite
        imageToChange.sprite = sprites[currentSpriteIndex];
    }

    void Update()
    {
        // Check if the left mouse button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            // Move to the next sprite in the array
            currentSpriteIndex++;

            // Ensure we do not exceed the array bounds
            if (currentSpriteIndex >= sprites.Length)
            {
                currentSpriteIndex = 0; // Reset to the first sprite if the end is reached
            }

            // Change the image sprite to the next one in the array
            imageToChange.sprite = sprites[currentSpriteIndex];
        }
    }
}

This is the code I have currently, but it's flawed and I'm not sure how to fix it. I don't want it to change when the person clicks down on the mouse like it is with the text, and how it is with the images currently. That messes up the pacing if someone clicks more than once per line. How would I go about changing this?

I'm making a 2D game using Unity and I want to implement cutscenes that change over at certain lines.

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

public class CgChanger : MonoBehaviour
{
   public Image imageToChange; // Reference to the UI Image component
    public Sprite[] sprites; // Array of sprites to cycle through`

    private int currentSpriteIndex = 0;

    void Start()
    {
        if (imageToChange == null || sprites == null || sprites.Length < 6)
        {
            Debug.LogError("ImageToChange or Sprites are not assigned or Sprites array has fewer than 6 elements.");
            return;
        }

        // Initialize with the first sprite
        imageToChange.sprite = sprites[currentSpriteIndex];
    }

    void Update()
    {
        // Check if the left mouse button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            // Move to the next sprite in the array
            currentSpriteIndex++;

            // Ensure we do not exceed the array bounds
            if (currentSpriteIndex >= sprites.Length)
            {
                currentSpriteIndex = 0; // Reset to the first sprite if the end is reached
            }

            // Change the image sprite to the next one in the array
            imageToChange.sprite = sprites[currentSpriteIndex];
        }
    }
}

This is the code I have currently, but it's flawed and I'm not sure how to fix it. I don't want it to change when the person clicks down on the mouse like it is with the text, and how it is with the images currently. That messes up the pacing if someone clicks more than once per line. How would I go about changing this?

Edit: I realize I wasn't as clear as I should have been with what I want. i have a certain amount of cutscene images that I want to stay on screen while certain lines are displayed, before switching to another image. If you've ever seen the cutscenes from the game Danganronpa, it's a bit like that

This code is C#, not the deprecated language UnityScript — Unity has not supported UnityScript in a decade, so please read tag descriptions carefully when posting.
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

I'm making a 2d2D game using Unity and I want to implement cutscenes that change over at certain lines.

using System.Collections;

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

public class CgChanger : MonoBehaviour { public Image imageToChange; // Reference to the UI Image component public Sprite[] sprites; // Array of sprites to cycle through

private int currentSpriteIndex = 0;

void Start()
{
    if (imageToChange == null || sprites == null || sprites.Length < 6)
    {
        Debug.LogError("ImageToChange or Sprites are not assigned or Sprites array has fewer than 6 elements.");
        return;
    }

    // Initialize with the first sprite
    imageToChange.sprite = sprites[currentSpriteIndex];
}

void Update()
{
    // Check if the left mouse button is pressed
    if (Input.GetMouseButtonDown(0))
    {
        // Move to the next sprite in the array
        currentSpriteIndex++;

        // Ensure we do not exceed the array bounds
        if (currentSpriteIndex >= sprites.Length)
        {
            currentSpriteIndex = 0; // Reset to the first sprite if the end is reached
        }

        // Change the image sprite to the next one in the array
        imageToChange.sprite = sprites[currentSpriteIndex];
    }
}

}

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

public class CgChanger : MonoBehaviour
{
   public Image imageToChange; // Reference to the UI Image component
    public Sprite[] sprites; // Array of sprites to cycle through`

    private int currentSpriteIndex = 0;

    void Start()
    {
        if (imageToChange == null || sprites == null || sprites.Length < 6)
        {
            Debug.LogError("ImageToChange or Sprites are not assigned or Sprites array has fewer than 6 elements.");
            return;
        }

        // Initialize with the first sprite
        imageToChange.sprite = sprites[currentSpriteIndex];
    }

    void Update()
    {
        // Check if the left mouse button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            // Move to the next sprite in the array
            currentSpriteIndex++;

            // Ensure we do not exceed the array bounds
            if (currentSpriteIndex >= sprites.Length)
            {
                currentSpriteIndex = 0; // Reset to the first sprite if the end is reached
            }

            // Change the image sprite to the next one in the array
            imageToChange.sprite = sprites[currentSpriteIndex];
        }
    }
}

I'm making a 2d game using Unity and I want to implement cutscenes that change over at certain lines.

using System.Collections;

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

public class CgChanger : MonoBehaviour { public Image imageToChange; // Reference to the UI Image component public Sprite[] sprites; // Array of sprites to cycle through

private int currentSpriteIndex = 0;

void Start()
{
    if (imageToChange == null || sprites == null || sprites.Length < 6)
    {
        Debug.LogError("ImageToChange or Sprites are not assigned or Sprites array has fewer than 6 elements.");
        return;
    }

    // Initialize with the first sprite
    imageToChange.sprite = sprites[currentSpriteIndex];
}

void Update()
{
    // Check if the left mouse button is pressed
    if (Input.GetMouseButtonDown(0))
    {
        // Move to the next sprite in the array
        currentSpriteIndex++;

        // Ensure we do not exceed the array bounds
        if (currentSpriteIndex >= sprites.Length)
        {
            currentSpriteIndex = 0; // Reset to the first sprite if the end is reached
        }

        // Change the image sprite to the next one in the array
        imageToChange.sprite = sprites[currentSpriteIndex];
    }
}

}

I'm making a 2D game using Unity and I want to implement cutscenes that change over at certain lines.

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

public class CgChanger : MonoBehaviour
{
   public Image imageToChange; // Reference to the UI Image component
    public Sprite[] sprites; // Array of sprites to cycle through`

    private int currentSpriteIndex = 0;

    void Start()
    {
        if (imageToChange == null || sprites == null || sprites.Length < 6)
        {
            Debug.LogError("ImageToChange or Sprites are not assigned or Sprites array has fewer than 6 elements.");
            return;
        }

        // Initialize with the first sprite
        imageToChange.sprite = sprites[currentSpriteIndex];
    }

    void Update()
    {
        // Check if the left mouse button is pressed
        if (Input.GetMouseButtonDown(0))
        {
            // Move to the next sprite in the array
            currentSpriteIndex++;

            // Ensure we do not exceed the array bounds
            if (currentSpriteIndex >= sprites.Length)
            {
                currentSpriteIndex = 0; // Reset to the first sprite if the end is reached
            }

            // Change the image sprite to the next one in the array
            imageToChange.sprite = sprites[currentSpriteIndex];
        }
    }
}
Source Link

How would I make a "slideshow" cutscene?

I'm making a 2d game using Unity and I want to implement cutscenes that change over at certain lines.

using System.Collections;

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

public class CgChanger : MonoBehaviour { public Image imageToChange; // Reference to the UI Image component public Sprite[] sprites; // Array of sprites to cycle through

private int currentSpriteIndex = 0;

void Start()
{
    if (imageToChange == null || sprites == null || sprites.Length < 6)
    {
        Debug.LogError("ImageToChange or Sprites are not assigned or Sprites array has fewer than 6 elements.");
        return;
    }

    // Initialize with the first sprite
    imageToChange.sprite = sprites[currentSpriteIndex];
}

void Update()
{
    // Check if the left mouse button is pressed
    if (Input.GetMouseButtonDown(0))
    {
        // Move to the next sprite in the array
        currentSpriteIndex++;

        // Ensure we do not exceed the array bounds
        if (currentSpriteIndex >= sprites.Length)
        {
            currentSpriteIndex = 0; // Reset to the first sprite if the end is reached
        }

        // Change the image sprite to the next one in the array
        imageToChange.sprite = sprites[currentSpriteIndex];
    }
}

}

This is the code I have currently, but it's flawed and I'm not sure how to fix it. I don't want it to change when the person clicks down on the mouse like it is with the text, and how it is with the images currently. That messes up the pacing if someone clicks more than once per line. How would I go about changing this?