Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by orenko · Sep 15, 2016 at 09:04 AM · scripting problemprefabscriptingbasicstriggersfunction call

Call function when prefab is collided

Hello, I have a game with a spaceship travelling between 2Dprefabs. This is the code that generates the prefabs:

 Material material = new Material(Shader.Find("Sprites/Default"));
 material.SetTexture("_MainTex", textures2D[i]);
 GameObject quad = GameObject.CreatePrimitive(PrimitiveType.Quad);
 quad.name = textures2D[i].name;
 quad.transform.position = Vector3.zero;
 quad.transform.localScale *= 20;
 
 MeshRenderer meshRenderer = quad.GetComponent<MeshRenderer>();
 meshRenderer.sharedMaterial = material;
 GameObject prefab = PrefabUtility.CreatePrefab(prefabPath + textures2D[i].name + ".prefab", quad, ReplacePrefabOptions.Default);

I want something to happen when the spaceship is collided with the prefab. I've tried to add a script, with the function : onCollisonEnter, and attach it the the prefab, in the inspector. (I added the script component to the prefab). However, it didn't work, and the function was not called. I tried to use the function onTriggerEnter but it also didn't work.

Does someone no what I should do? All I want is to call a function when the prefab is collided. Attached a screenshot of the defined prefab.

Thanks!

alt text

prefab.png (20.5 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by TommyHansenKiloo · Sep 15, 2016 at 10:37 AM

At least one of your object need to have a rigidbody attached for oncollisionEnter to happen

Comment
Add comment · Show 1 · 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 orenko · Sep 15, 2016 at 10:46 AM 0
Share

@$$anonymous$$myHansen$$anonymous$$iloo Thanks, Isn't the $$anonymous$$esh Collider should be enough? When I'm trying to add the line:

 quad.AddComponent<Rigidbody2D> ();

I get the error:

Can't add component 'Rigidbody2D' because it conflicts with the existing '$$anonymous$$eshCollider' derived component!

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

95 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Build a House using code and prefabs 0 Answers

Instantiate Prefab From Script Inside A Canvas 2 Answers

Can some one help me with my C# script? (HUD) 0 Answers

Acceleration issue 0 Answers

How do I make a script wait X time? 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