[SOLVED]
Hello everyone. A long time has passed since my last post, and here we are again.
Let me explain you my issue. I am creating a 2D shooter game (on top view). I created a class, SurvivorClass, that looks like this:
using System;
using UnityEngine;
namespace AssemblyCSharp
{
public class SurvivorClass
{
private WeaponClass weapon;
private GameObject survivorGameObject;
private Transform survivorTransform;
private int health;
private float survivorVelocity;
private float survivorDrag;
private float survivorAngularDrag;
public SurvivorClass (GameObject survivorGameObject, WeaponClass weapon, int health, float survivorVelocity)
{
this.weapon = weapon; //Default constructor. Pistol.
this.survivorGameObject = survivorGameObject;
this.health = health;
this.survivorVelocity = survivorVelocity;
this.survivorTransform = this.survivorGameObject.transform;
this.survivorDrag = this.survivorVelocity / 5;
this.survivorAngularDrag = this.survivorDrag * 5;
survivorTransform.rigidbody2D.drag = survivorDrag;
survivorTransform.rigidbody2D.angularDrag = survivorDrag;
}
public float getSurvivorVelocity() {return this.survivorVelocity;}
public Vector3 getSurvivorPos() {return this.survivorTransform.position;}
public Vector3 getSurvivorAngle() {return this.survivorTransform.eulerAngles;}
public WeaponClass getWeapon() {return this.weapon;}
public void setSurvivorVelocity(float survivorVelocity) {this.survivorVelocity = survivorVelocity;}
public void setSurvivorAngle(Vector3 v3TouchedPos) {
Vector2 v2TouchedPos = new Vector2 (v3TouchedPos.x, v3TouchedPos.y);
Vector3 v3CurrentPos = Camera.main.WorldToScreenPoint (survivorTransform.position);
Vector2 v2CurrentPos = new Vector2 (v3CurrentPos.x, v3CurrentPos.y);
Vector2 v2DisplacDir = v2TouchedPos - v2CurrentPos;
Vector2 v2DisplacDir_Norm = v2DisplacDir.normalized;
survivorTransform.right = v2DisplacDir_Norm;
}
public void moveSurvivor(Vector2 v2TouchedPos) {
Vector3 v3CurrentPos = Camera.main.WorldToScreenPoint (survivorTransform.position);
Vector2 v2CurrentPos = new Vector2 (v3CurrentPos.x, v3CurrentPos.y);
Vector2 v2DisplacDir = v2TouchedPos - v2CurrentPos;v2DisplacDir.Normalize ();
survivorTransform.rigidbody2D.velocity = v2DisplacDir * this.survivorVelocity;
}
public void moveSurvivorAndLookAtPos(Vector3 v3TouchedPos) {
Vector2 v2TouchedPos = new Vector2 (v3TouchedPos.x, v3TouchedPos.y);
setSurvivorAngle (v3TouchedPos);
moveSurvivor(v2TouchedPos);
}
}
}
Now, I create another script attached to my main camera so that, when the game opens, it creates a reference to SurvivorClass and initializes it, like so:
using UnityEngine;
using System.Collections;
using AssemblyCSharp;
public class StartGame : MonoBehaviour {
public GameObject survivorGameObject;
private WeaponClass weapon;
private SurvivorClass survivor;
// Use this for initialization
void Start () {
weapon = new WeaponClass ();
survivor = new SurvivorClass (survivorGameObject, weapon, 10, 6.0f);
}
}
And now my question. When I move my character (survivor) by using other script (Move.cs), I need to know what her/his velocity is by retrieving this data from the instance created at the start of the game (by using: survivor.getSurvivorVelocity()).
Another scenario in which I would need to access this instance would be in the case the player takes a “power-up” drink: in this case I would need to modify its speed, health, … and so on.
So, in a nutshell: I create an instance of SurvivorClass at the beginning of the game, and I need to access this instance from all other scripts of my game, modifying/reading it.
How can I achieve this?
Thank you in advance!
