- Home /
The question is answered, right answer was accepted
Sphere with texture using C# scripting
I'm newbie in unity.
I've started 2D project and tried to create globe object through scripting and got fail. The problem the texture doesn't apply my dynamically rendered sphere object.
using UnityEngine;
using System.Collections;
public class Hyper : MonoBehaviour {
// Use this for initialization
void Start () {
Debug.Log("2D Globe Projection");
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position = new Vector3(0, 0, 0);
Texture tex = Resources.Load("earth", typeof(Texture)) as Texture;
sphere.GetComponent<Renderer>().material.SetTexture("_MainTex", tex);
}
// Update is called once per frame
void Update () {
}
}
This script is attached to camera object.
Please help to solve that problem.
Note: The texture 'earth' is located in Assets/Resources folder.
Thank you!
Answer by neptolab · Sep 19, 2016 at 06:00 AM
Texture runtimeTexture = (Texture) Resources.Load("earth");
Material runtimeMaterial = new Material(Shader.Find("VertexLit"));
runtimeMaterial.SetTexture("_MainTex", runtimeTexture);
sphere.GetComponent<Renderer>().material = runtimeMaterial;
Solved by me.
Hey,
So how would the code look if i wanted to load different textures after different conditions are met like
if (something happens) { load face1 } then if(something else happens) { load face2 }
and so forth
Follow this Question
Related Questions
Resources does load up in the editor but not in build 0 Answers
Multiple 360 VR video player in Unity 0 Answers
Instantiated object not showing in scene or hierarchy 2 Answers
When generating prefab, i cant load images from either resource or assetbundle 2 Answers
How do I set a sprite to a sphere? 0 Answers