Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Sastrei · Apr 26, 2012 at 04:56 PM · variableclassclasses

Access class variables of selected object

Hi all,

I am trying to make a space-based RTS game. So there will be multiple ship types, each with it's own variable values, but the same number and type of variables for each ship type. I've got the scripts set up the way I want them, but I'm stuck at the last step, which is accessing the variables of an object I select in a scene. Including a little bit more code than necessary so you can see where I put the code I've entered.

ShipObjDefTest.js - Ship variables definition. Attached to the camera for now.

 #pragma strict
 
 public class ShipObj {
     var type : String;
     var size : String;
     var speed : float;
     var cost : float;
 }
 
 function Start () {
 }
 
 function Update () {
 }

TestScout.js - Values for this ship type. This is attached to a prefab of the same name.

 #pragma strict
 
 var newShip = new ShipObj();
 newShip.type = "Scout";
 newShip.size = "Small";
 newShip.speed = 30;
 newShip.cost = 100;
 
 function Start () {
 }
 
 function Update () {
 }

QuickShipPicker.js - My testing script. Attached to the camera.

 #pragma strict
 
 var hitinfo : RaycastHit;
 var ship;
 
 function Start () {
 }
 
 function Update () {
     if (Input.GetMouseButtonDown(0)) {
         var cameraRay : Ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         if (Physics.Raycast (cameraRay, hitinfo)) {
             ship = hitinfo.transform.parent;
             print(ship);
             //print(TestScout.newShip.type);
             //print(TestScout.newShip.speed);
         }
     }    
 }

The commented lines in QuickShipPicker.js show what I want to do, but the type of ship (TestScout in the commented lines) will not always be the same.

Any help is much appreciated!

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Lo0NuhtiK · Apr 26, 2012 at 06:40 PM

You'll have to use a GetComponent in your raycast stuff to get the script from the object you're hitting and access it.
e.g.

 if(Physics.Raycast(ray, hit)){
    var ship : GameObject = hit.collider.gameObject ;
    var shipScript : TestScout ;
    shipScript = ship.GetComponent(TestScout) as TestScout ;
    Debug.Log(shipScript.shipVariable.classVariable) ;
 }

...something along those lines.

Comment
Add comment · Show 3 · Share
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 Sastrei · Apr 26, 2012 at 06:52 PM 0
Share

I appreciate you taking the time to respond. Unfortunately, my exact problem is that the name of the script will not stay the same. I need to get to the ShipObj instance inside of each script. Otherwise I'll have to hardcode the game for each and every ship type (scout, corvette, frigate, destroyer, etc).

Edit: Replacing TestScout with ShipObj compiles but returns: Object reference not set to an instance of an object

avatar image Lo0NuhtiK · Apr 26, 2012 at 07:28 PM 0
Share

Why not have it as one script? You're wanting to set the values in the inspector right?
ShipAttributes (or whatever you call the script)


    class ShipObj{
       var type : String ;
       var whatever : int ;
    }
    var Ship : ShipObj = ShipObj() ;

Then you'd put the script on each ship, set their stuff in the inspector, then assign the script like in the above example when you ray-hit it and access it that way. Debug.Log(shipScript.Ship.type) ;
....you could probably just skip the raycast stuff your doing also and use On$$anonymous$$ouseDown ins$$anonymous$$d, then wouldn't need the multiple scripts and such.

avatar image Sastrei · Apr 27, 2012 at 05:33 AM 0
Share

I'll give that a try and see if I can set the variables in each copy of the script in each ship subfolder ins$$anonymous$$d of through the editor. I don't like setting values there, too easy to lose track of the information for me.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Access to a variable inside a C# class 2 Answers

Editting a custom javascript class in the inspector 1 Answer

Accessing classes class from other script 2 Answers

Can I choose what to pass into .getcomponent<{VARIABLE}>(); 1 Answer

Accesing custom class object from another script 1 Answer


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