|
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. TestScout.js - Values for this ship type. This is attached to a prefab of the same name. QuickShipPicker.js - My testing script. Attached to the camera. 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!
(comments are locked)
|
|
You'll have to use a GetComponent in your raycast stuff to get the script from the object you're hitting and access it. ...something along those lines. 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
Apr 26 '12 at 06:52 PM
Sastrei
Why not have it as one script? You're wanting to set the values in the inspector right? 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) ;
Apr 26 '12 at 07:28 PM
Lo0NuhtiK
I'll give that a try and see if I can set the variables in each copy of the script in each ship subfolder instead of through the editor. I don't like setting values there, too easy to lose track of the information for me.
Apr 27 '12 at 05:33 AM
Sastrei
(comments are locked)
|
