Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by HausLord · May 09, 2012 at 11:00 PM · prefabpositionvector3magnitude

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

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image HausLord · May 10, 2012 at 08:44 PM 0
Share

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

avatar image HausLord · May 10, 2012 at 09:00 PM 0
Share

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.

avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges