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 Fetdressing · Dec 17, 2013 at 08:37 PM · arrayfunctionprojectile

Spaceship game, Pick weapon

Hello! I am making a 2D spaceship shooter game, seen from above, kinda like the old meteor game. I am trying to make a menu where you can equip your ship with weapons of your own choice. I am having problems with trying to move the picked weapon (a projectile gameobject) from my menu script to my fire script. I am using arrays in my both scripts but I get errors here and there. Any help would be much appreciated! Here are the scripts:

 private var playerShip : GameObject;
 
 private var weaponTypes : int[];
 public var plasma : GameObject;
 public var bluePlasma : GameObject;
 
 
 private var pekare : int = 0;
 
 
 private var widthe : int = 200;
 private var heighte : int = 80;
 private var placeEquip : Rect = new Rect(widthe/4, heighte/2, widthe, heighte * 8);
 
 private var spot1 : Rect;
 private var spot2 : Rect;
 private var spot3 : Rect;
 private var spot4 : Rect;
 private var spot5 : Rect;
 private var spot6 : Rect;
 private var spot7 : Rect;
 private var spot8 : Rect;
 private var spot9 : Rect;
 
 
 private var placeUnequip : Rect = new Rect(widthe/4 + widthe + 5, heighte/2, widthe, heighte * 8);
 
 private var plasmaEquipped : boolean = false;
 private var bluePlasmaEquipped : boolean = false;
 private var redPlasmaEquipped : boolean = false;
 private var greenPlasmaEquipped : boolean = false;
 
 
 function Start () {
     playerShip = GameObject.FindGameObjectWithTag("ThePlayer").gameObject;
     
     
         spot1 = new Rect(10, 20, 100, 20);
         spot2 = new Rect(10, 45, 100, 20);
         spot3 = new Rect(10, 70, 100, 20);
         spot4 = new Rect(10, 95, 100, 20);
         spot5 = new Rect(10, 115, 100, 20);
         spot6 = new Rect(10, 135, 100, 20);
         spot7 = new Rect(10, 150, 100, 20);
         spot8 = new Rect(10, 175, 100, 20);
         spot9 = new Rect(10, 120, 100, 20);
 
     
 }
 
 
 function OnGUI(){
 
     
     if(GUI.Button(Rect(Screen.width/2, Screen.height/2, 100, 100), "Send")){
         for(var i = 0; weaponTypes[i] != null; i++){
             playerShip.transform.FindChild("Weapons").gameObject.GetComponent(FireScript).receiveValue(weaponTypes[i], i, 0.4, 900);
         }
     }
 
 placeEquip = GUI.Window (0, placeEquip, EquipFunction, "Weapons Avaible");
     placeUnequip = GUI.Window(1, placeUnequip, UnEquipFunction, "Weapons Equipped");
 }
 
 function EquipFunction(windowID : int){
 if(plasmaEquipped == false){
     if (GUI.Button (spot1, "Plasma")){
             weaponTypes[pekare] = 1; 
             pekare++;
             plasmaEquipped = true;
             
         }
         }
 if(bluePlasmaEquipped == false){        
     if (GUI.Button (spot2, "Blue Plasma")){
             weaponTypes[pekare] = 2;
             pekare++;
             bluePlasmaEquipped = true;
         }
         }
 }
 
 
 function UnEquipFunction(windowID : int){
 if(plasmaEquipped == true){
     if (GUI.Button (spot1, "Plasma")){
             
             plasmaEquipped = false;
         }
         }
 if(bluePlasmaEquipped == true){        
     if (GUI.Button (spot2, "Blue Plasma")){
             bluePlasmaEquipped = false;
         }
         }
 }
 

Shoot script:

 #pragma strict
 public var slots : GameObject[];
 
 private var weapon : GameObject[];
 private var forceProj : float[];
 private var reloadTime : float[];
 private var timerRel : float[];
 
 /**
 public var plasmaProj : GameObject;
 
 public var bluePlasmaProj : GameObject;
 **/
 
 public var plasma : GameObject;
 public var bluePlasma : GameObject;
 
 
 //private var slot1Wep : GameObject;
 
 //public var slot1Reload : float = 1;
 private var timer : float;
 /**
 public var force1 : float;
 public var force2 : float;
 public var force3 : float;
 public var rel1 : float;
 public var rel2 : float;
 public var rel3 : float;
 private var timerRel1 : float;
 private var timerRel2 : float;
 private var timerRel3 : float;
 **/
 function Start () {
 //timerRel = slotsReloadTime;
 
 timerRel[0] = 0;
 }
 
 function Update () {
         //shoot part
     
     //slot1 weapon
     
     
     if(Input.GetKey(KeyCode.Space)){
         var arr = slots;
         for(var i = 0; i < arr.Length; i++){
         
     if(timerRel[i] < Time.time){
         timerRel[i] = 0;
 
             
         var copyPar = Instantiate(weapon[i], slots[i].transform.position, this.transform.rotation);
         
         copyPar.rigidbody.velocity = this.transform.forward * forceProj[i];
         
         Destroy(copyPar, 5);
         timer = Time.time + 0.2;
         timerRel[i] = Time.time + reloadTime[i];
     }
     
 
     }
     }
 }
 
 function receiveValue(projectileType : int, spotPut : int, rel : float, force : float){
     
     if(projectileType == 1){
         weapon[spotPut] = plasma;
     }
     else if(projectileType == 2){
         weapon[spotPut] = bluePlasma;
     }
     reloadTime[spotPut] = rel;
     
     forceProj[spotPut] = force;
     
 }
 

Sorry for unused variables and such which might make it harder to read.

Comment
Add comment · Show 1
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 Kiloblargh · Dec 17, 2013 at 09:00 PM 0
Share

TL;DR.

You should be a lot more specific about what errors you're getting and just post the relevant part of the script that is generating those errors.

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by ThatsAMorais · Dec 17, 2013 at 09:20 PM

I see what you're trying to do, I think. You mentioned some problems and errors you were having. Comment to this with the types of errors you're seeing, exactly.

Here's what I gather... You have a GUI button and two windows displaying some options. I'm a little confused on the "Send" button because it looks like you call receiveValue for each of the weapon choices when it is pressed.

Nonetheless, my answer to your issue relates to the two "weapons" arrays. The weapons array in "Shoot" only serves the function of storage of a set of GameObjects, but a better alternative would be a data-structure that allows you to index that set of object instances easily. So, instead of weapons : GameObject[], I'd use a Dictionary. For example

 // declaration:
 var myDictionary = new Dictionary.<KeyType,ValueType>();
 
 // and a real-world declaration example (where 'Person' is a custom class):
 var myContacts = new Dictionary.<string,Person>();
 
 // insert or change the value for the given key
 myDictionary[anyKey] = newValue;                 
 
 // retrieve a value for the given key
 var thisValue = myDictionary[theKey];      
 
 // get the number of items in the Hashtable
 var howBig = myDictionary.Count;                 
 
 // remove the key & value pair from the Hashtable, for the given key.
 myDictionary.Remove(theKey);

With this data-structure, you still have a set of GameObjects, but you can reference them according to their name. For example, in "Shoot" you would initialize the Dictionary like this.

 function Start()
 {
   myDictionary["Plasma"] = plasma;
   myDictionary["Blue Plasma"] = bluePlasma
 }

So, now that you can access the weapon objects using a string, you should use these strings in your other script with OnGUI() to make selections. For example...

 var weapons : List.<string>();
 
 ...
 
 function Start()
 {
    weapons.Add("Plasma");
    weapons.Add("Blue Plasma");
 }
 
 ...
 
 for(var weaponName:String in weapons){
   if(GUI.Button(Rect(Screen.width/2, Screen.height/2, 100, 100), weaponName)){
      playerShip.transform.FindChild("Weapons").gameObject.GetComponent(FireScript).receiveValue(weaponName, 0.4, 900);
   }
 }

You've got to work out one other issue with which one of these two scripts will "contain" the weapons data. In other words, You don't want weapon-name strings defined in one script that may or may not correspond to the keys of a dictionary of another script.

Get back to me if you need other help, but I think you probably just have to get some of this basic stuff figured out. Figure out how to use this language effectively. Learn about encapsulation a little because you're probably going to want to have the weapon types defined in one place, and just pass the float value to the "Shoot" script on whatever weapon object is firing it.

Comment
Add comment · Show 4 · 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 Fetdressing · Dec 17, 2013 at 11:58 PM 0
Share

That seems like a really great way to solve this! Never worked with dictionary before but will definetly try my best using it, thanks alot! :) The send was just like a test bottom so that I could send over my weapons choice over to the firingscript and see if it received it.

avatar image ThatsAMorais · Dec 18, 2013 at 05:23 AM 0
Share

Dictionary is a great way to store enumerated data that is not in a specific order. It grows dynamically (although how it is implemented will ultimately deter$$anonymous$$e the cost of this). Grants constant time access to elements. The "weapons["weaponName"].value" syntax is very readable, better than "weaponArray[0]". An alternative to String type keys are enumerations.

avatar image ThatsAMorais · Dec 18, 2013 at 05:27 AM 0
Share

I am interested in "answering" this question, so what are the errors you're seeing? Does this question have an answer?

avatar image Fetdressing · Dec 28, 2013 at 04:06 PM 0
Share

Sorry been very busy, but I think I afterall won't use this in my game. I do however have a script that fires between an array off weapons but it just won't be choose-able in the game. Here is the firing script if anyone is interrested, pretty simple but it satisfy my need atm :)

 public var slots : GameObject[];
 
 public var weapon : GameObject[];
 
 
 /**
 public var plasma : GameObject;
 public var bluePlasma : GameObject;
 public var redPlasma : GameObject;
 public var greenPlasma : GameObject;
 **/
 //private var slot1Wep : GameObject;
 
 //public var slot1Reload : float = 1;
 private var timer : float;
 /**
 public var force1 : float;
 public var force2 : float;
 public var force3 : float;
 public var rel1 : float;
 public var rel2 : float;
 public var rel3 : float;
 private var timerRel1 : float;
 private var timerRel2 : float;
 private var timerRel3 : float;
 **/
 public var force : float[];
 public var rel : float[];
 public var timerRel : float[];
 
 private var wepShot : int = 0;
 private var arrLength : int;
 
 function Start () {
 //timerRel = slotsReloadTime;
 
 //timerRel[0] = 0;
 
 //var arr = weapon;
 arrLength = weapon.Length;
 Debug.Log(arrLength.ToString());
 for(var x = 0; x < arrLength; x++){
     timerRel[x] = 0.0;
 }
 }
 
 function Update () {
         //shoot part
     
     //slot1 weapon
     
     
     if(Input.Get$$anonymous$$ey($$anonymous$$eyCode.Space)){
         //var arr = slots;
         for(var i = 0; i < arrLength; i++){
         
     if(timerRel[i] < Time.time){
         //timerRel[i] = 0;
 
             
         var copyPar = Instantiate(weapon[i], slots[i].transform.position, this.transform.rotation);
         
         copyPar.rigidbody.velocity = this.transform.forward * force[i];
         
         Destroy(copyPar, 5);
         timerRel[i] = Time.time + 0.05;
         timerRel[i] = Time.time + rel[i];
     }
     
 
     //}
     }
 }
 }

Thanks anyways for trying to help :)

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

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to destroy all the items with one function? 2 Answers

Passing an array to a function by reference? 1 Answer

Passing an Array 1 Answer

Remeber the value between frame Update() 1 Answer

Calling functions from a class - Curious issue 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