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!
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
@$$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
Follow this Question
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