x


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), "");
    }
}

}

more ▼

asked May 09 '12 at 11:00 PM

HausLord gravatar image

HausLord
15 1 3

(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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 
    }
    ...
more ▼

answered May 09 '12 at 11:12 PM

aldonaletto gravatar image

aldonaletto
41.5k 16 42 197

My 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...

May 10 '12 at 08:44 PM HausLord

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.

May 10 '12 at 09:00 PM HausLord
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered May 10 '12 at 07:29 PM

HausLord gravatar image

HausLord
15 1 3

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1260
x887
x670
x580
x28

asked: May 09 '12 at 11:00 PM

Seen: 419 times

Last Updated: May 11 '12 at 02:21 AM