- Home /
same script for two scenes, how can i call a function for particular scene with out affecting that function for another scene??
same script for two scenes, how can i call a function for particular scene with out affecting that function for another scene?? In below code, second scene will load when button clicked, i want to call DisplayInRandom() function for second scene in OnMouseDown(). Initially i should also call DisplaysInSequence() function for first scene in OnMouseDown(). But i'm able to call only one function for both scenes in OnMouseDown(). I want to call specific function for particular scene by attaching same script for both scenes.
using System.Collections; using System.Collections.Generic; using System.Linq; using UnityEngine; using UnityEngine.SceneManagement;
public class DisplaySingleImage : MonoBehaviour { public Sprite[] Images; List Sprites; List Scenes; int NextValue = 0;
void Start () {
Images = Resources.LoadAll<Sprite>("Sprites");
Sprites = Images.ToList();
}
private void OnMouseDown()
{
DisplayInSequence();
}
public void ToRandom()
{
SceneManager.LoadScene("DisplaySingleImageRandom");
}
public void ToSequence()
{
SceneManager.LoadScene("DisplaySingleImage");
}
public void DisplayInSequence()
{
NextValue++;
if (NextValue == Images.Length)
NextValue = 0;
GetComponent<SpriteRenderer>().sprite = Images[NextValue];
Debug.Log(Images[NextValue]);
}
public void DisplayInRandom()
{
if (Sprites.Count == 0)
{
Sprites = Images.ToList();
}
NextValue = UnityEngine.Random.Range(0, Sprites.Count);
GetComponent<SpriteRenderer>().sprite = Sprites[NextValue];
Debug.Log(Sprites[NextValue]);
Sprites.RemoveAt(NextValue);
}
}
Your answer
Follow this Question
Related Questions
function calls for each frame in the scene 0 Answers
Running a script without being attached to an object? 2 Answers
SceneManager.LoadScene not working ONLY when it is in IEnumerator functions 1 Answer
how to keep the script working after scene load? 1 Answer
How to keep my Prefab from Restiing 0 Answers