Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PasoUnleashed · Apr 08, 2016 at 06:50 AM · scripting problemscriptingbasics

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?

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Ali-hatem · Apr 08, 2016 at 10:04 AM 0
Share

cheek this click

avatar image Gruffy · Apr 08, 2016 at 10:45 AM 0
Share

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

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges