- Home /
Importing a class into multiple scripts
Hi, so I'm trying to create a class called gameItem, and make instances of it in other scripts, Example: using UnityEngine; using System.Collections;
public class gameItem : MonoBehaviour {
int damage;
int critical;
string name;
gameItem(int d, int c, string n)
{
damage = d;
critical = c;
name = n;
}
gameItem()
{
damage = 1;
critical = 50;
name = "Wooden Sword";
}
}
And then import it to another script:
using UnityEngine;
using System.Collections;
public class playerController : MonoBehaviour {
gameItem[] inventory = new gameItem[invSize];
}
So how do I go about doing that?
Hey PasoUnleashed, in order to use the game item class in another script would require declaration first like so ...
gameItem gi;
then instantiation alongside the initialisation through teh constructor you are supplying from gameItem class... gl = new gameItem();<--- inside those aprenthesis you need to fill in some arguments if you look at your class they are the ones in that constructors parenthesis. So, @ constructor you make parameters for any user of it to fill out @ useage point, you fill in the parameters supplied with arguments...
You currently have two constructors there. A default one & a parameterised one.. Before we go on, what is it that you wnat this class to do? If you can provide your reasoning we may be able to devleop the class you have above, but at this stage your class is relatively sound except there are no properties there to obtain the variables inoyur gameItem class once they have been established. And the default constructor simply does the same thing each time it is called which is a good trait for a constructor obvioulsy but when called all gameItems will be called "wooden Sword" have a critical of 50 and a damage of 1...is this what you really wnated? Hence the request to lay out what it is that you want to do with the gameItem class :) Cheers bud Gruffy
Your answer
Follow this Question
Related Questions
Getting Sprites to Face Eachother (building on 2D Platformer tut) 0 Answers
How can I activate a GameObject Component of type script? 2 Answers
Simple (2D)Uscript movement. 0 Answers
How to modify the below script to manual transmission of gears? 0 Answers
Attacking mulitple targets simultanously 2 Answers