Newbie with several questions.

Hello all, I’m new to Unity3d, and have a few questions on how to setup a project. I’m an amateur C# programmer, and I’ve used a few other Game Engine tools, but I’ve never gotten good at any of them. Unity looks great so far, but I’m having a few issues…

First, a short description of my demo project. The game I’m making to help me learn Unity basics is a simple button-pusher RPG combat simulation. You get GUI buttons for Attack, Spells, Flee, etc. you click attack, do damage to the enemy, who then attacks back, etc. I’ll add more as I go, but basic combat interaction is my first step. Also I should note I’m not using graphics. Just text for now.

So that being said. I need a Player Object with some variable, an Enemy Object with some variable, and some buttons to push. I’ve figured out the GUI buttons, it was simple and works great. But as for the objects, that’s where I have a problem.

  1. If I create a Player GameObject and assign it a Player script, it creates a Player class. Great. I can setup that class like I would in C# and add my vars and player functions. But… where do I instantiate that class? Where do I actually CREATE the player?

  2. If I create this Player Object in a Loading scene and then have the next scene be the combat, I understand I’d need to call DontDestroyOnLoad() for the Player. Do I call this function and pass the Player class to it, or do I pass it the Instantiated object?

I actually think that’s about it for now. I should be able to finish my basic project pretty easily once I figure out how to organize things in Unity.

Oh, one more Question.

  1. What books on Unity would you all recommend for newbs wanting to make RPGs?

Thanks!

Chris V.

I thought I had figured this out on my own, but I’m running into issues still. Here’s what I decided on. Please guide me as to if this is close to right.

I have a Loader scene that will only ever be called once. I created a Game Manager object with a GM script on that scene. My GM Script right now does the following

	public CHero hero;   //Player Class defined in it's own script

	void Start () 
	{
		DontDestroyOnLoad(this);
		hero = new CHero();
		hero.hp = 10;
	}

is the ‘this’ right? Should I put something else there such as the name of the object or the player instance?

Next, in a second script on the next scene, I try to access hero. I can’t seem to do that.

GUI.Label (new Rect(30,30,200,20), GM.hero.hp.ToString ()); //GM.hero.hp says it needs an instance of an Object.

Please help…

Thanks!

Still trying to work this out on my own while waiting for an answer. I think I’m close…

I’ve found that when you create a GameObject and apply a script to it. Unity automatically Instances the class in that script and that object basically becomes an instance of that class. So, I reworked my GameLoader scene to include a Hero Object with a Hero.cs script and an Enemy object with an Enemy.cs script. Now, in my next scene, I have a combat function that needs to access both of those objects and the class variables from the previous scene. (they are set to DontDestroyOnLoad() )

when I try to access the objects from the previous scene, I’m getting errors. Here’s how I’m trying it.

public class GUI_Combat : MonoBehaviour 
{
	public CHero hero;
	
	void Start()
	{
		hero = FindObjectOfType(typeof(CHero));
	}
	void OnGUI()
	{
		GUI.Label (new Rect(30,30,200,20), hero.heroname);
	}

which is throwing the error

GUI_Combat.cs(10,10): Error CS0266: Cannot implicitly convert type 'UnityEngine.Object' to 'CHero'. An explicit conversion exists (are you missing a cast?) (CS0266) (Assembly-CSharp)

CHero is the name of my hero class, not the name of the Hero Object in the first scene. I’m confused… What am I doing wrong here?

    public class GUI_Combat : MonoBehaviour
    {
        public CHero hero;
       
        void Start()
        {
            hero = FindObjectOfType(typeof(CHero)) as CHero;
        }
        void OnGUI()
        {
            GUI.Label (new Rect(30,30,200,20), hero.heroname);
        }

Thanks nbg_yalta. That works well.