- Home /
Inheritance (Printing a toString for subclasses)
My apologies. Could this thread please be removed. Or at the very least the code posted.
Answer by qJake · Mar 24, 2010 at 03:13 AM
Looking it over again... you said the problem was "flickering" text, like multiple text objects were being drawn at the same time, correct?
When using inheritance, if you create an "Adult", for example, it also creates a "Person". So if you have two "Adults", then, technically, you also have two "Person" objects (programming objects, not game objects). I'm assuming that if you have multiple villagers, then you have multiple instances of the "Person" class, and therefore, multiple text objects all trying to draw in the same position, at the same time.
Answer by qJake · Mar 24, 2010 at 02:28 AM
I'm not exactly clear on what's going on... but my best suggestion is: are you sure you're using OnMouseDown() properly? Are you sure you don't want to be using this instead?:
function Update()
{
if(Input.GetKeyDown("mouse 0"))
{
toString();
}
}
Additionally, you should look into using String.Format() instead of just concatenating everything. It makes your code a lot easier to read. (The code is in .NET languages (like C#), but it can be used with Javascript in the same way.) Just a suggestion ;)
Answer by qJake · Mar 24, 2010 at 08:13 PM
What you probably need to do is have some sort of flag (read: boolean variable) to determine whether or not the GUIText should draw or not. Here's one way of doing it:
- In the
Person
class, have a bool variable, something like "drawDebugText". Make sure it's false to start with. - When the user clicks on a villager, loop through every villager and set "drawDebugText" to false. Then set the one that you just clicked on, to true.
- In
OnGUI()
, have something likeif(drawDebugText) { // draw here // }
If you're not sure about how to loop through all villagers, consider adding a tag or component that will allow you to find all of them at once.
(Also, hooray for 3 answers :) )
It doesn't matter, either way it sounds like you're going to have to end up looping through all the game objects. If I were you, I would just end up using the console and Debug.Log() or print(), it's a hell of a lot easier than doing all this work just for debug information. ;)
Your answer
Follow this Question
Related Questions
An OS design issue: File types associated with their appropriate programs 1 Answer
Visual Studio Code Mac (1.29.1) problem with class inheritance Unity (2018.2.17f1) 0 Answers
Unity exports my derived class, but not its base class (C# OOP) - Is there a workaround? 0 Answers
How to work with a list of base class, if a variable is only found in some subclasses? 2 Answers
How to handle different GUI.Toogles in one GUI-Script to execute a specific function? 1 Answer