• 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
0
Question by g0tNoodles · Oct 02, 2012 at 09:17 PM · triggerconvertingtostring

Converting int to string does not fire off triggers?

Hello,

In my game, I have some buildings that will be placed by the player and these are given a random ID which is also the buildings name (converted from int to string). When the NPC runs into the buildings collider it triggers the NPCs attack function. Oddly, when the buildings have their original name this trigger works fine but when the name is changed to a random number, it does not....

This is the code I am using to set the random name/number and what I have set to trigger it.

     static public string id;
 
     // Use this for initialization
     void Start () {
         id = Random.Range(0,500).ToString();
         
         gameObject.name = id;
         Debug.Log ("ID = " + id);
     }
     
     void OnTriggerStay(Collider other)
         {
                 if(other.name == targetItem.name)
                 {
 isAttacking = true;
                         animation.CrossFade("up_sword_action_copy");
                         Instantiate(particleObject, new Vector3(targetItem.transform.position.x,targetItem.transform.position.y + 4,targetItem.transform.position.z), targetItem.transform.rotation);            
                 }
         
             }


Thanks very much!

Edit: Yeah, sorry about that! This is the whole script for the NPC...it's a bit messy while I try to find out how to work this damage/trigger bit.

 using UnityEngine;
 using System.Collections;
 
 public class ObjectAvoidance : MonoBehaviour {
     
     public enum Buildings
     {
         Home,
         GunnerTurret,
         FlameTurret
     }
     
     public float moveSpeed = 10.0f;
     public float attackSpeed;
     public float attackTimer;
     public float coolDown;
     
     public int Damage;
     
     public GameObject particleObject;
     
     public bool isAttacking = false;
     public float distance;
     
     GameObject[] playerItems;
     public GameObject targetItem;
     public GameObject targetToAttack;
     RaycastHit hit = new RaycastHit();
     
     public float val;
     
     void Awake()
     {
         PopulateItemList();
         targetItem = GetClosest(playerItems);
     }
 
 //Enemies list of player objects to find
     void PopulateItemList() {
         playerItems = GameObject.FindGameObjectsWithTag("Building");    
     }
     
 //Finds closest player game object to attack
     GameObject GetClosest(GameObject[] items) {
         
         GameObject closest = null;
         
         float minDist = Mathf.Infinity;
         
         Vector3 currentPos = transform.position;
         
         foreach(GameObject i in items)
         {
             float dist = Vector3.Distance (i.transform.position, currentPos);
             
             if(dist < minDist) {
                 closest = i;
                 minDist = dist;
             }
         }
         
         return closest;
     }
 
     // Use this for initialization
     void Start () {
         attackTimer = 0;
         coolDown = 1.5f;
     }
     
     void FixedUpdate()
     {
         if(targetItem == null) {
             PopulateItemList();
             targetItem = GetClosest(playerItems);
         }
         
         distance = Vector3.Distance(targetItem.transform.position, transform.position);
         
         rigidbody.constraints = RigidbodyConstraints.FreezePositionY;
         rigidbody.constraints = RigidbodyConstraints.FreezeAll;        
     }
     
     // Update is called once per frame
     void Update () {
         
         targetToAttack = targetItem;
         
         if(isAttacking != true)
             MoveEnemy();
         
         if(attackTimer > 0)
         {
             attackTimer -= Time.deltaTime;
         }
         
         if (attackTimer < 0)
             attackTimer = 0;
             
     }
     
 //Used for enemy movement
     void MoveEnemy()
     {
 //        transform.position = Vector3.Lerp(transform.position, playerHome.position, moveSpeed);
         //transform.position = Vector3.Lerp(start.position, end.position, Time.time);
         
         Vector3 dir = (targetItem.transform.position - transform.position).normalized;
         
         
         
         if(Physics.Raycast(transform.position, transform.forward, out hit, 20))
         {
             Debug.DrawLine(transform.position, hit.point, Color.red);
             dir += hit.normal*20;
         }
         
         Vector3 leftR = transform.position;
         Vector3 rightR = transform.position;
         
         leftR.x -=2;
         rightR.x +=2;
         
         if(Physics.Raycast(leftR, transform.forward, out hit, 20))
         {
             Debug.DrawLine(leftR, hit.point, Color.red);
             dir += hit.normal*20;
         }
         
         if(Physics.Raycast(rightR, transform.forward, out hit, 20))
         {
             Debug.DrawLine(rightR, hit.point, Color.red);
             dir += hit.normal*20;
         }
         
         Quaternion rot = Quaternion.LookRotation(dir);
         
         transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
         transform.position += transform.forward * 15 * Time.deltaTime;
         
         animation.Play("run");
     }
 
 //Used for switching between the attack and move methods
     void OnTriggerStay(Collider other)
     {
         isAttacking = true;
         if(other.name == targetItem.name)
         {
             if(attackTimer <= 0 && isAttacking == true)
             {
                 animation.CrossFade("up_sword_action_copy");
                 attackTimer = coolDown;
                 Instantiate(particleObject, new Vector3(targetItem.transform.position.x,targetItem.transform.position.y + 4,targetItem.transform.position.z), targetItem.transform.rotation);
 //                PlayerObjectHealth.curHealth -= 10;
                 hit.collider.SendMessage(DamageMethodName, Damage, SendMessageOptions.DontRequireReceiver);
             }            
         }
 
     }
     
     public void Attack(float frame)
     {    
         Vector3 dir = (targetItem.transform.position - transform.position).normalized;        
         Quaternion rot = Quaternion.LookRotation(dir);        
         transform.rotation = Quaternion.Slerp(transform.rotation, rot, Time.deltaTime);
         
         rigidbody.constraints = RigidbodyConstraints.FreezeAll;
     }
 }
 
Comment
Add comment · Show 2
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 dannyskim · Oct 02, 2012 at 09:20 PM 0
Share

Where is targetItem set, and what is it set to. You're setting the gameObject.name in Start, and then you're checking the collision against targetItem, so there's some info we're missing.

avatar image g0tNoodles · Oct 02, 2012 at 09:33 PM 0
Share

Edited the question to add the whole script in.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can't click gameobject when over another trigger? 1 Answer

Restraining movement to camera bounds 1 Answer

Shuttle Runs & Counter 0 Answers

enemy aggro region definitive answer 1 Answer

strange behavior on collision 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