Hi all, i really need a bit help. I have a university project but i am not into programming (more of an art school) and i need help with a c# script.
I have made a script that when my first person camera moves near an object the object will be lighted. My theme is a museum thats why. Now i need to make when he gets near the object a small description to come up and when he leaves to disappear.
I have done the colliders and such just need a script in c# if possible.
Thanks!
You could use a script that contains a GUI.Label() call and a saved string. Then all you’d need to do is change that scripts string whenever you enter a collision.
i.e.
Every Object has string desc.
The Player has a currentDesc string
OnCollisionEnter sets the Players currentDesc string to desc.
You have a GUI.Label(new Rect(0,0,250,50), currentDesc) somewhere on the player.
When you leave the Collision set the string to “” so that no description is shown.
Thanks for the reply but i still don’t get it so much. First of all is GUI.label c#? Because our stubborn teacher doesnt want javascript :S And secondly can you tell me how to save a string in every object?
My initial thought was to have a 3d text on a transparent cube, set the cube at scale 0,0,0 and on enter the cube at normal settings. On exit scaling back to 0,0,0.
But my programming skills suck and havent found how to do that correctly.
All the functions are in both Java and C#.
My method places it on the screen like a HUD, so that it moves with the camera and is drawn last.
The easiest way to save a string to an object is to do it on a monobehaviour.
Place this on the Object
using UnityEngine;
using System.Collections;
public class NameStorage: MonoBehaviour
{
public string description;
}
Place this on the player
using UnityEngine;
using System.Collections;
public class LabelWriter: MonoBehaviour
{
public Rect labelSpot = new Rect(0,0,250,100);//Upper left of screen
public string toLabel = "";
void OnGUI()
{
GUI.Label(labelSpot, toLabel);
}
void OnCollisionEnter(Collision collisionInfo)
{
GameObject target = collisionInfo.collider.gameObject;
toLabel = (target.GetComponent(typeof(NameStorage)) as NameStorage).description;
}
void OnCollisionExit(Collision collisionInfo)
{
toLabel = "";
}
}
Granted this is written in the forum box off the top of my head but that should get the basic function working.
The lable should be in the upper right, move the Rect parameters around to adjust it to a more appropriate spot.
Thank you very much for the reply. I put the scripts to where they should be, but it doesn’t seem to work. If i place a word in the laber writer it will show it all the time without changing it to the word of the description. I tried it without putting words too but that doesnt show anything Sorry for all the trouble mate and thanks in advance if you reply.
Ahh, If your Colliders are set to isTrigger, you will need to change the OnCollisionEnter and OnCollisionExit to OnTriggerEnter and OnTriggerExit.
Yeah the labelWriter public string is a bit of a quick hack in that if you write in it it will always be visible and could cause errors later, but it’s the quick and dirty way of getting your result.
But try the Trigger functions, and if not post a sample of your collision scripts, I can try to see what you are using to detect the collisions in the first place.
using UnityEngine;
using System.Collections;
public class BoxTrigger : MonoBehaviour {
public Light light;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update ()
{
}
void OnTriggerEnter(Collider collider)
{
light.enabled=true;
}
void OnTriggerExit(Collider collider)
{
light.enabled=false;
}
}
this is the script for turning the lights on and off. My first person camera has a rigid body, and i have made transparent cubes around the statues to be triggers. I changed the oncollision to ontrigger but still nothing
I found the mistake on changing the OnCollision to OnTrigger needed to change the collisioninfo and such to collider. It works nice! Only think it puts out an error:
NullReferenceException: Object reference not set to an instance of an object
LabelWriter.OnTriggerEnter (UnityEngine.Collider collider) (at Assets/LabelWriter.cs:19)
It sounds like an object does not have NameStorage is my guess.
Try using
NameStorage storage = other.GetComponent(typeof(NameStorage)) as NameStorage;
if(storage!=null)
{
toLabel = storage.description;
}
Or something like it to see that NameStorage exists before using it.