- Home /
Collision with my first object of array or second...
Hi, in last two days i have begin to program in C# and try to create arrays. So my question is pretty simple: How can I program to detect a collision with my first GameObject of array. Like this:
using UnityEngine;
using System.Collections;
public class InOutVehicle : MonoBehaviour {
public GameObject[] EnterCollider = new GameObject[0];
// I remove other irrelevants parts of script, so that's it
void OnCollisionEnter(Collision col)
{
if(col.gameObject = EnterCollider[0] )
{
if(Input.GetKeyDown(KeyCode.E))
{
//Do Stuff
}
}
}
}
//Sorry for english, I'm from Brazil;
You probably want to use Input.Get$$anonymous$$ey($$anonymous$$eyCode keyCode).
You will need to create your array bigger than size '0'. The chances of you pushing the key as the colliders enters the collision with the other collider is very low so I suggest using on collision stay ins$$anonymous$$d... And lastly, maybe watch a few tutorials to get a basic understanding of unity collisions :) good luck!
Answer by lorenzofman2 · Mar 18, 2015 at 11:59 PM
Oh! My God! Look my error:
if(col.gameObject = EnterCollider[0]
and the right is:
if(col.gameObject == EnterCollider[0]
Ow man sorry for this stupid question. It's a newbie error but Unity doesn't debug the error correctly, in the console appeared: error CS0200: Property or indexer `UnityEngine.Collision.gameObject' cannot be assigned to (it is read only)
Your answer
![](https://koobas.hobune.stream/wayback/20220613190441im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Sphere detection system 2 Answers
trying to define the last object I collided with and store it as a variable? 2 Answers
Detecting if object in array collides with another object in same array 1 Answer
Collecting Array items in order 1 Answer
What is the best way to instatiate an array of connected GameObjects? 0 Answers