- Home /
Problem with Prefab
I have a problem with my prefab. I import my model and created a prefab "Player". Again i did the same step to the "Enemy", but i need check the distance of player with enemy and when i use
Vector3 D = player.position - tranform.position(script attached in enemy)
it check the prefab position and not the gameObject in scene. Then i use it
Player = GameObject.FindWithTag("Player").transform;
and work with player.... but my "Enemy" is several equal in scene, and i not instantiate them. It is fixed before. They have a function that does count in a variable "Player" and one bool variable. Basically when comes to near of enemy, it count a alert, and enable a GUI placed on player script.
What i need? I need a solution for them to function independent.
Here my enemy script.
using UnityEngine;
public class CDA_01 : MonoBehaviour {
#region Status
private enum NPCStates
{
Searching,
Walking,
InAlert
}
private NPCStates state = NPCStates.Searching;
#endregion
private Transform enemy;
MyPerson scriptPlayer;
public void Start()
{
scriptPlayer = GameObject.FindWithTag("Player").GetComponent<MyPerson>();
enemy = GameObject.FindWithTag("Player").transform;
}
public void Update()
{
if (state == NPCStates.Searching)
{
//Standing
animation.Play("Standing");
SearchingPlayer();
}
}
private void SearchingPlayer()
{
Vector3 D = enemy.position - transform.position;
if (D.magnitude <= 8)
{
**//HERE IS THE PROBLEM**
scriptPlayer.nearToCDA = true;
scriptPlayer.countAlert += 0.4f;
}
else
{
scriptPlayer.nearToCDA = false;
}
}
}
Here part of player script
using UnityEngine;
using System;
[RequireComponent(typeof(CharacterController))]
public class MyPerson : MonoBehaviour {
public float countAlert = 0;
public bool nearToCDA = false;
........
**//HERE THE PROBLEM**
public void OnGUI()
{
if (nearToCDA)
{
GUI.Box(new Rect(Screen.width / 2 - 80, Screen.height / 2 + 150, countAlert * 2, 20), "");
}
}
}
Answer by aldonaletto · May 09, 2012 at 11:12 PM
You're assigning the Player to the variable enemy, thus the distance between the player and the enemy will always be zero. You don't need to find the enemy in its own script: just use the property transform:
... private Transform enemy; MyPerson scriptPlayer;
public void Start()
{
scriptPlayer = GameObject.FindWithTag("Player").GetComponent< MyPerson>();
enemy = transform; // this object is the enemy, thus just get its transform
}
...
$$anonymous$$y problem is similar it http://answers.unity3d.com/questions/245542/need-help-understanding-scripted-prefab-behavior-w.html
I need to controll the prefabs independently...
Olá! vi sua pagina e você é de São Paulo. Também sou, tem como você me passar seu contato pra poder me ajudar? esse meu problema é de um trabalho para faculdade! Obrigado.
Answer by HausLord · May 10, 2012 at 07:29 PM
Yes, it worked, but i need show my GUI. My GUI showed when i stay near the enemy, but it isnt worked. My GUI just showed in near the first enemy prefab on scene. If im move to third enemy prefab, the variable countAlert works and GUI not.
What i want to do? If i stay near 1 enemy prefab, show GUI and countAlert. If i stay near 2 enemy prefab, my countAlert x2 and show GUI, but independent of which of prefabs, everyone has to count and everyone has to show GUI.
note: i use the variable nearToCDA for show and hide the GUI.
Your answer
Follow this Question
Related Questions
Clone of rigid body prefab destruction if at certain position 3 Answers
How can I translate my prefab on Instantiate? 1 Answer
Instantiate problem prefab wrong position in child object.... 0 Answers
Don't know why this script doesn't work. 1 Answer
Offset against Rotated Object,Applying Offset to a Rotated Transform 1 Answer