Hello everybody.
Sorry for spamming with questions, but it seems i can’t grasp how to reference objects, particularly prefabs.
I will try to describe the situation at my best.
I have 2 objects, both prefabs (Player and Rock).
Attached to “Player” there is a script called “player”:
using UnityEngine;
using System.Collections;
public class Player : MonoBehaviour
{
public int lives = 3; //default number of lives
public int keys = 0; //default number of keys
public int score = 0; //default score
public bool isDead = false; //default dead status
public int weapontype = 1; //default weapon type
public int power=4;
// Use this for initialization
void Start()
{
power = 4;
}
// Update is called once per frame
void setPower()
{
power = 4;
}
public void changePower()
{
power--;
}
void Update()
{
Debug.Log(power);
}
}
Attached to Rock there is a script called “ChangePower”:
using UnityEngine;
using System.Collections;
public class ChangePower : MonoBehaviour
{
public Player _player;
// Use this for initialization
void Start()
{
Player _player = GetComponent<Player>();
}
// Update is called once per frame
void Update()
{
if(Input.GetKeyDown(KeyCode.Space))
{
_player.changePower();
}
}
}
In the Inspector, i “drag reference” both player and rock into the respective “scripts” slots, so i can see that i have created the reference (the inspector let me create prefabs references only with other prefabs):i then drag the prefabs into the scene, then start the game…the variable “power” isn’t getting updated, like the objects aren’t active.
Now…if after placing the prefabs in the scene (from the prefab folder) i create the references for both scripts using the objects from the scene (and NOT from the prefab folder) the variable “power” is updated properly…like the objects are now active.
Is it supposed to be like that or i don’t understand how to use prefabs properly(and reference them through scripts)?
Hopefully i have explained the situation in a clear way.
I also got a feeling that this is the same problem i was having before in my previous posts…not sure why!
Thank you