Search Unity

How to change variable from another script [FOR ANDROID]

Discussion in 'Scripting' started by Erickmwc, Mar 7, 2015.

  1. Erickmwc

    Erickmwc

    Joined:
    Feb 2, 2015
    Posts:
    16
    Hello, what I'm tring to do is to when the player destroys the enemy object, it get some experience. Now what I'm tring to do is to when on one "Upgrade Window", if the player press one button (eg. shootfaster button) it'll increase the shooting speed.
    I've done the experiente "system" following this tutorial http://unity3d.com/learn/tutorials/projects/space-shooter/counting-points but I cannot make it work with the button. Its maybe something relationed to the type, I keep getting the error "Cannot implicitly convert type [] to `UnityEngine.GameObject' "
    Here's the button code: (I'm new)
    Code (CSharp):
    1. public class ShootSpeed : MonoBehaviour {
    2.  
    3.     public int speedValue;
    4.     private GameObject shotspeed;
    5.  
    6.  
    7.     void Start(){
    8.  
    9.         GameObject g = GameObject.FindWithTag ("MainBull"); // MainBull is the object that contain the ShotMov
    10.  
    11.         shotspeed = g.GetComponent <ShotMov>(); //ShotMov is the second script, where the wanted variable is
    12.  
    13.     }
    14.  
    15.     public void ShootButton () { //Ill call this function when the button is TOUCHED
    16.         shotspeed.AddSpeed(speedValue);
    17.     }
    18.  
    19. }
    And here's the bullets code, that is inserted on the MainBulled code:
    Code (CSharp):
    1. public class ShotMov : MonoBehaviour {
    2.  
    3.     private int speed;
    4.     public int shotspeed;
    5.  
    6.  
    7.     void Start () {
    8.         rigidbody.velocity = transform.up * shotspeed;
    9.     }
    10.     public void AddSpeed (int newSpeedValue)
    11.     {
    12.         speed+= shotspeed + 1; //Everytime I touch the button, the speed increases by 1
    13.     }
    14.  
    15.  
    16. }
    Thanks in advance
     
  2. Nanako

    Nanako

    Joined:
    Sep 24, 2014
    Posts:
    1,047
    You're increasing speed, but shotspeed is the value that you're actually using.

    Also, you're increasing speed by the amount of shotspeed every time, plus one.
    Also, speed is a reserved word. stop using it, use something else.

    The += modifier means "Make whatever is on the left side equal to itself, plus whatever is on the right side"
    So what you've got there is equivilant to writing speed = speed + shotspeed + 1

    You're not even using this speed variable at all, i don't see a need for it. just change that line to
    shotspeed += 1;

    Or you could even use shotspeed++;
    The ++ modifier means "Make whatever is on the left side equal to itself plus one".. i think it's faster in situations where you're just incrementing something by 1. (maybe that's wrong)
     
  3. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Hi,
    In your shootspeed class you made one small error

    Code (CSharp):
    1.  
    2. private GameObject shotspeed;
    3. //Since this will be your script reference you will want it to be this
    4. private ShotMov shotspeed;
    5.  
     
  4. Erickmwc

    Erickmwc

    Joined:
    Feb 2, 2015
    Posts:
    16
    When I used the private ShotMov shotspeed instead of private GameObject shotspeed the class error dissapear. Thanks for that, but now when it shoots, nothing changes, heres the new code with somethings changed according to you and Nanako
    Code (CSharp):
    1. public class ShootSpeed : MonoBehaviour {
    2.  
    3.     //public int speedValue;
    4.     //private GameObject shotspeed;
    5.     private ShotMov shotspeed;
    6.  
    7.     void Start(){
    8.    
    9.         GameObject g = GameObject.FindWithTag ("MainBull"); // MainBull is the object that contain the ShotMov
    10.    
    11.         shotspeed = g.GetComponent <ShotMov>(); //ShotMov is the second script, where the wanted variable is
    12.    
    13.     }
    14.  
    15.     public void ShootButton () { //Ill call this function when the button is TOUCHED
    16.         shotspeed.AddSpeed();
    17.  
    18.     }
    19.  
    20.  
    21. }
    and ShotMov script
    Code (CSharp):
    1.  
    2. public class ShotMov : MonoBehaviour {
    3.  
    4.     public int shotspeed;
    5.  
    6.  
    7.     void Start () {
    8.         rigidbody.velocity = transform.up * shotspeed;
    9.     }
    10.     public void AddSpeed ()
    11.     {
    12.         shotspeed+= 1;
    13.     }
    14.  
    15.  
    16. }
    I've added the ShotSpeed script to the button and created and event trigger to when I touch the button, the shotspeed should increase by 1 but nothing is happening.
     
    Last edited: Mar 7, 2015
  5. Erickmwc

    Erickmwc

    Joined:
    Feb 2, 2015
    Posts:
    16
    If I put one bullet on the screen it works but only for that bullet, but looks like the prefab do not change the initial value when it instantiate a new bullet
     
  6. lordconstant

    lordconstant

    Joined:
    Jul 4, 2013
    Posts:
    389
    Right now you are just looking for an object with the tag "MainBull" when you first load the ShootSpeed script, i am assuming you want to handle multiple bullets as they spawn, so something like this should work:

    if you add using System.Collections.Generic; to the top of the script and make your shotmov variable a list you will be able to add the bullets to the list and update their speeds.

    Code (CSharp):
    1.  
    2. List<ShotMov> shotSpeed;
    3.  
    4. void  Start(){
    5. shotSpeed = new List<ShotMov>();
    6. }
    7.  
    Then when you spawn a bullet just add it to the list
    Code (CSharp):
    1.  
    2. ShotMov myBullet = Instantiate(bullet, position, rotation) as ShotMov;
    3.  
    4. shotSpeed.Add(myBullet);
    5.  
    Now in shoot button just loop through the list

    Code (CSharp):
    1.  
    2. foreach(ShotMov curBull in shootSpeed){
    3. curBull.AddSpeed();
    4. }
    5.  
    Some of this is pseudo code so you will have to change it to work with your game.

    List Tutorial - http://unity3d.com/learn/tutorials/modules/intermediate/scripting/lists-and-dictionaries

    This should help with understanding lists aswell