- Home /
Switching From one texture to another
hi, In my game,I want to make a intro scene beofore the game start's...here's my problem how can i change texture form one to another in runtime.. i made a script,am a modeler who don't know scripting :/
here's my script,can anyone please correct this :-
var anim1 : Texture;
var anim2 : Texture;
var anim3 : Texture;
var anim4 : Texture;
function Start () {
renderer.material.mainTexture = anim1;
yield WaitForSeconds(2);
renderer.material.mainTexture = anim2;
yield WaitForSeconds(2);
renderer.material.mainTexture = anim3;
yield WaitForSeconds(2);
renderer.material.mainTexture = anim4;
yield WaitForSeconds(2);
Application.LoadLevel(2);
}
thank you :)
Answer by Eric5h5 · Dec 24, 2012 at 09:38 AM
Your script works fine as-is, but it could be written as:
var textures : Texture[];
var delayTime = 2.0;
function Start () {
for (var i = 0; i < textures.Length; i++) {
renderer.material.mainTexture = textures[i];
yield WaitForSeconds (delayTime);
}
Application.LoadLevel (2);
}
Answer by Griffo · Dec 24, 2012 at 08:20 AM
Try this, drop you object to change texture on into myCharacter in the inspector.
var myCharacter:Transform;
var anim1 : Texture;
var anim2 : Texture;
var anim3 : Texture;
var anim4 : Texture;
function Start () {
ChangeTexture();
}
function Update(){
}
function ChangeTexture(){
myCharacter.renderer.material.mainTexture = anim1;
yield WaitForSeconds(2);
myCharacter.renderer.material.mainTexture = anim2;
yield WaitForSeconds(2);
myCharacter.renderer.material.mainTexture = anim3;
yield WaitForSeconds(2);
myCharacter.renderer.material.mainTexture = anim4;
yield WaitForSeconds(2);
Application.LoadLevel("Level Two");
}
Start can be a coroutine, and you don't need (and shouldn't have) an empty Update function, since it creates unnecessary overhead.
Answer by Maulik2208 · Dec 24, 2012 at 08:31 AM
using UnityEngine;
using System.Collections;
public class NewBehaviourScript : MonoBehaviour
{
public Texture2D anim1;//This is your textures have to assign in inspector
public Texture2D anim2;
public Texture2D anim3;
public Texture2D anim4;
public GameObject Player;//this is the game object you have to assign in
// inspector....you can change the name as per your need
// Use this for initialization
void Start ()
{
StartCoroutine ("ChangeTexture");
}
IEnumerator ChangeTexture()
{
Player.renderer.material.mainTexture = anim1;
yield return new WaitForSeconds(5);
Player.renderer.material.mainTexture = anim2;
yield return new WaitForSeconds(5);
Player.renderer.material.mainTexture = anim3;
yield return new WaitForSeconds(5);
Player.renderer.material.mainTexture = anim4;
yield return new WaitForSeconds(5);
Application.LoadLevel(2);
}
}
This is the script for C# which is doing well......here gameobject should be replace with the name of your gameobject.......Cheers......Enjoy.....If found useful then don't forget to mark.....
how to add this in C#
var anim1 : Texture; var anim2 : Texture; var anim3 : Texture; var anim4 : Texture;
sorry, i have no idea about C#
I've seen it create a lot of confusion with people who are new to program$$anonymous$$g, since they're having a hard enough time with one language. So it would generally be better to hold off answering in a different language, except as a last resort if the question has gone unanswered for a while.
Your answer
![](https://koobas.hobune.stream/wayback/20220613085853im_/https://answers.unity.com/themes/thub/images/avi.jpg)