add 3d text through script on a object
is it possible to add a 3d text on an object which was spawned through a script for example,
GameObject sphere = GameObject.CreatePrimitive (PrimitiveType.Sphere);
can you add a 3d text above it through script?
Answer by NerdClown · Sep 16, 2016 at 08:47 AM
If I were you, I'd make a prefab with your primitive and a text mesh placed under it, and then instantiate the prefab from script.
You CAN also create your sphere, create an empty game object, put a text mesh component on there, put a mesh renderer component on there, set the text game object's parent to the sphere game object's transform and then put in the local coordinates for the text mesh that will make it be above the sphere. I guess that would work, never tried it. Seems like a lot of work!
In most cases I think the prefab solution would be better. There's some docs on instantiating prefabs at runtime here
my program will spawn multiple spheres at random locations the location is set, is there a way to do this purely through script?
There definitely is! At work now, I will try to find time to provide an example later, if no-one beats me to it :)
Here we go
using UnityEngine;
using System.Collections;
public class SpawnStuff : $$anonymous$$onoBehaviour {
public float x$$anonymous$$in = -50, y$$anonymous$$in = -50, z$$anonymous$$in = -50,
x$$anonymous$$ax = 50, y$$anonymous$$ax = 50, z$$anonymous$$ax = 50;
public GameObject objectToSpawn;
public int numberOfObjectsToSpawn = 40;
void Start ()
{
for (int i = 0; i < this.numberOfObjectsToSpawn; i++)
{
Vector3 position = new Vector3(Random.Range(x$$anonymous$$in, x$$anonymous$$ax), Random.Range(y$$anonymous$$in, y$$anonymous$$ax), Random.Range(z$$anonymous$$in, z$$anonymous$$ax));
GameObject instantiatedObject = (GameObject)Instantiate(this.objectToSpawn, position, Quaternion.identity);
instantiatedObject.name = "Object number " + i;
}
}
}
You need to make a prefab (drag a sphere from the hierarchy into the project window). Then you need to put the script on something (for instance an empty game object) and then drag your new prefab onto the right place on the script on the empty game object.
You CAN also do it with only CreatePrimitive, but I'd recommend the instantiate prefab way because you have so many more options for changing your prefabs without having to change your already-working code.
Your answer
Follow this Question
Related Questions
how do i rotate an object? C# 1 Answer
I want to spawn objects probabilistically. 0 Answers
Swing on z axis 0 Answers
Destroy moving objcts through touch on screen 0 Answers
I'm having a problem while jumping an object in Unity 5 0 Answers