- Home /
Trying to make an Upgradable weapons screen
So I made a script called upgrade, shown below:
using Unity Engine;
using System.Collections;
public class upgrade : MonoBehavior {
public class Accessor : MonoBehaviour {
void Start ()
{
GameObject Player = GameObject.Find("Player");
PlayerShooting PlayerShooting = Player.GetComponent<PlayerShooting>();
PlayerShooting.damagePerShot += 40;
}
}
}
And I also made a script called upgradescreen with a public void upgrade () that has GetComponent(); So what i'm trying to have happen is make it so when you press the upgrade button and then start, you'll be take to the level with an upgraded weapon, but its not working. What exactly am I doing wrong?
Answer by Mmmpies · Jun 01, 2016 at 10:35 AM
I'm trying to work out what you actually want. I think all you want is to get a return from this script with a higher rate of damage.
All you'd really need for that is...
using Unity Engine;
using System.Collections;
public class upgrade : MonoBehavior {
void Start ()
{
GameObject Player = GameObject.Find("Player");
PlayerShooting PlayerShooting = Player.GetComponent<PlayerShooting>();
}
public float UpgradeWeapon()
{
float PassBack = PlayerShooting.damagePerShot += 40;
return (PlassBack);
}
}
But when you say you open a new scene you do know a different scene will reset your scripts. You have to use data persistence to save information between scenes
Answer by aWolfKing · Jun 01, 2016 at 10:55 AM
PlayerShooting PlayerShooting = Player.GetComponent(); is the error line i think; you named the class the same as the variable, you should change the name of the variable becouse now the script is trying to access the class and change the value and not change the component's value.
(you did: 'class_name' 'class_name' =somthing , should be: 'class_name' 'variable_name' = somthing)
For example: 'PlayerShooting VarPlayerShooting = Player.GetComponent<PlayerShooting>();
VarPlayerShooting.damagePerShot += 40;'