- Home /
Extend from box. Notes
I need to program a game in which you can pick-up notes. I want to make a class that extends from Cube gameobject and add a variable noteText to it. I am new to unity so I have no idea whatsoever on how to do this. Can someone give me a hint?
Answer by Molix · Apr 12, 2010 at 09:06 PM
You won't extend from cube; you'll create a script (which extends from MonoBehaviour) and then attach it to any object (like a cube).
e.g. (C#)
public class PickupNote : MonoBehaviour { public string noteText = "";
public void OnTriggerEnter( Collider col ) { // check if other object is a player // e.g. if( col.gameObject.tag == "Player" )
Debug.Log("You picked up a note: " + noteText);
Destroy(gameObject); // kill the pickup object now that you have the note
} }
Yes but ehmm I want a note with variable text... I want to create like a new note ( var note :PickupNote = new PickupNote("This is a test"); ) Like that...
If you look at $$anonymous$$olix's code above you will see that he has in fact declared a variable called noteText to hold a String for the content of the note.
I don't use C# myself, but you would likely access that variable just like any other. You would have to look at a C# equivalent of Accessing Other Gameobjects (http://tinyurl.com/y2a7qyw) if you are addressing the variable from another script on another gameObject.
Answer by Jeroen 1 · Apr 12, 2010 at 11:21 PM
Yes but ehmm I want a note with variable text... I want to create like a new note ( var note :PickupNote = new PickupNote("This is a test"); ) Like that..
Also, it is bad form to post follow up questions as an Answer. If you have a query, post a comment or make edits to your question.