- Home /
Prefab, referencing them, using their methods:bug or intentional?
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
Answer by NoseKills · Jan 03, 2015 at 11:23 PM
both player and rock into the respective "scripts" slots,
I don't know if I understand completely since I can't see anything that could be dragged into Player script to be assigned. On ChangePower there is a Player variable that can be assigned by dragging.
But if I understand the rest correctly what you describe is expected behavior. If you drag a prefab from the "Project" view into a "slot" (variable) on another prefab/script, that reference really points to the prefab in the Project view. That's what is happening in the first example you give. Even when you drag the prefabs into the scene, the dragged references in the scripts of the prefabs in the scene still point to the objects in project view. You can then use those references to make new objects (Instantiate new prefabs from project view into the scene) in your game.
In your second example you already Instantiate the prefabs by dragging them from project view into the scene. Then you make the references in the scene, so the references point to the actual "live" objects in the scene, and it all works as you'd expect.
The prefabs in project view are meant for re-using and if things would work as you expect in your first attempt, things would get really difficult if you were to drag more and a different number of players and rocks into the scene. Let's say you drag 2 "rocks" and 3 "player"s into the scene after making the references in project view. Which Player object would each rock point to now ? What if you'd only drag rocks into the scene...which players would the rocks reference then when there are none in the scene...
Thank you, that make sense. I found a way to pick the exact object i needed and then reference it; this way i can have multiple copies of the prefab that don't get into each other way since the "active" object can only be one. Thanks.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
[SOLVED] Prefab as copy not as reference 1 Answer
Distribute terrain in zones 3 Answers
Do you have to set a object reference for every script? 0 Answers
instantiate prefabs y+1 from last instintiated prefab 1 Answer