• 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 sethi-sahil27 · Apr 10, 2016 at 11:59 AM · collisionprefabsontriggerenter

PreFabs not colliding with other GameObjecs, but collide with themselves.

i'm fairly new to unity and currently in process making my first game using unity.

i have currently a player(GameObject: sphere) revolving around a centre point in circular motion.

Another enemy, it is a prefab and i instantiate after a random amount of time and move it towards the centre around which the player revolves.

code for the instantiation of the PreFab enemy.

 using UnityEngine;
  using System.Collections;
  
  public class enemy_movement : MonoBehaviour {
  
  
         public float targetTime;
          int i=0;
          int num=-1;
          int temp;
          public Transform[] patrolpoints;
          public Transform planetposition;
      public int movespeed;
      private int currentposition;
      public int size;
      GameObject[] enemy=new GameObject[500];
       GameObject PreFab;
    //  public GameObject deathparticles;
      
      // Use this for initialization
      void Start () {
      
      targetTime=random_value_float();
      PreFab=(GameObject)Instantiate(Resources.Load("enemy"));
      PreFab.name="enemy";
      //currentposition=0;
      }
      
      
      
     
     
 
      
      
      // Update is called once per frame
      void Update () {
      
      targetTime -= Time.deltaTime;
      
      if((targetTime<=0 && num<=1))
      {
      num=num+1;
      temp=random_value();
      
      //this is the error line
  
  
      enemy[num] = (GameObject)Instantiate(PreFab, patrolpoints[temp].position, Quaternion.identity);
      enemy[num].name="enemy";
      targetTime=random_value_float();
      
      }
      
          for(i=0;i<=num;i++)
          enemy[i].transform.position=Vector3.MoveTowards(enemy[i].transform.position, planetposition.position , movespeed*Time.deltaTime);
      }
      
      
      
      
      
      
      int random_value()
      {
      int num = Random.Range(0, patrolpoints.Length);
  return num;
      }
      
      float random_value_float()
      {
      float num = Random.Range(5, 30) * .1f;
  return num;
      }
     
  }

followed by the code of the player, that detects collision with the enemy prefab.

 using UnityEngine;
 using System.Collections;
 
 public class Playermovement : MonoBehaviour {
     
     public Transform playerpos;
     
     int flag=0;
     float angle =0;
     float speed=(4*Mathf.PI)/5 ;//2*PI in degress is 360, so you get 5 seconds to complete a circle
     float radius=1.8f;
     float x;
     float y;
 
     // Use this for initialization
     void Start () {
     
     }
     
      void OnTriggerEnter(Collider collision) 
     {
     Debug.Log ("Called by "+ transform.gameObject.name);
     
    if(collision.gameObject.name == "enemy")
       print("player hit " + collision.gameObject.name);
       
        //else 
      //{
       //   Debug.Log("Hit something else!"+ collision.gameObject.tag);
     // }
 }
     
     // Update is called once per frame
     void Update()
  {
     
       if(flag==0)
       {
       angle += speed*Time.deltaTime; //if you want to switch direction, use -= instead of +=
       //flag=1;
       }
       else if(flag==1)
       {
       angle -= speed*Time.deltaTime; //if you want to switch direction, use -= instead of +=
       //flag=0;
       }
       
      if (Input.GetKeyDown("space"))
     {
     if(flag==0)
     flag=1;
     
     else if(flag==1)
     flag=0;
     }
      
      x = Mathf.Cos(angle)*radius -1.06f ;
      //x=x-1.12;
      y = Mathf.Sin(angle)*radius -1.04f;
      //y=y-0.56;
      
       transform.position = new Vector3(x, y, 0);
       
       
            // print("up arrow key is held down");
  }
  
  
 }
 

now the issue

the enemy prefabs (that are instantaited) can collide with each other but not with the player.

if i place the enemy near the player (without instantaiting it) it gets collided with the player .

player is a sphere with isTrigger checked and Rigidbody component added,same goes for the enemy PreFab. and have tried both names and tags , nothing works.

any advice would be appreciated.

thanks!!

Comment
Add comment · Show 5
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 Cherno · Apr 10, 2016 at 12:04 PM 0
Share

Check the Physics settings, and make sure both layers can register collisions in the Collision $$anonymous$$atrix.

Show more comments

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Unable to get OnTriggerEnter to work.[JS] 2 Answers

How to only delete one of two collided objects? 1 Answer

OnTriggerEnter Bad Performance - Bad Code? 1 Answer

Child calling parent's OnTriggerEnter when no Rigidbody is attached to child 1 Answer

Collision between 2 prefabs. 0 Answers


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