- Home /
Question by
El Maxo · Feb 17, 2015 at 11:25 AM ·
c#collideronmousedown
OnMouseDown (Collider other) Not working
Hello all
I am trying to make it so when click on a certain object it will spawn a item there. The issue that I have is that this isn't working correctly. (when I click it isn't spawning, I added in a color change just to make sure my other code wasn't a issue). Below is my code. (sorry my description isn't much help)
using UnityEngine;
using System.Collections;
public class RoomSpawn : MonoBehaviour {
public GameObject Room1;
public GameObject Room2;
public Transform Room1Spawn;
public Transform Room2Spawn;
public GameObject Player;
void OnMouseDown (Collider other){
if (other.gameObject == Room1) {
Room1.GetComponent<Renderer>().material.color = Color.blue;
Instantiate (Player, Room1Spawn.position, Room1Spawn.rotation);
}
}
}
Comment
Just a quick question, is it possible to have On$$anonymous$$ouseDown with a collider ?
Best Answer
Answer by PvTGreg · Feb 17, 2015 at 11:28 AM
its OnMouseOver and you dont the collider other part
so in onmouseover you put if input.getbutton(mouse1) do your code check if its name ect
Im a little confused.
So I change it to :
void On$$anonymous$$ouseOver (){
if (gameObject == Room1) {
Room1.GetComponent<Renderer> ().material.color = Color.blue;
Instantiate (Player, Room1Spawn.position, Room1Spawn.rotation);
}
}
if (Input.GetButtonDown("Fire1"))
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Physics.Raycast(ray, out hit))
{
if (hit.collider.name = room1);
//do your code
}