I’m trying to do a tic tac toe game.In the code i put three variables
var x:GameObject;
var ball:GameObject;
var choice:GameObject;
When it will instantiate,it instantiate choice var:
var part=Instantiate(choice,transform.position,transform.rotation);
I create a OnGui function with two buttons to tweak between x or ball:
function OnGUI()
{
if (GUI.Button (Rect (10,75,50,50), "X")) {
choise=x;
}
if (GUI.Button (Rect (10,10,50,50), "Ball")) {
choise=ball;
}
}
But this code doesn’t works:( Where is the problem? Full Code
var x:GameObject;
var ball:GameObject;
var choice:GameObject;
var inserted=false;
function OnMouseDown()
{
put();
}
function put()
{
if(inserted==false)
{
var part=Instantiate(choice,transform.position,transform.rotation);
inserted=true;
}
}
function OnGUI()
{
if (GUI.Button (Rect (10,75,50,50), "X")) {
choise=x;
}
if (GUI.Button (Rect (10,10,50,50), "Ball")) {
choise=ball;
}
}
var x:GameObject;
var ball:GameObject;
var choice:GameObject;
var inserted=false;
//function OnMouseDown()
//{
//put();
//}
function put()
{
if(inserted==false)
{
var part=Instantiate(choice,transform.position,transform.rotation);
inserted=true;
}
}
function OnGUI()
{
if (GUI.Button (Rect (10,75,50,50), "X")) {
choise=x;
put();
}
if (GUI.Button (Rect (10,10,50,50), "Ball")) {
choise=ball;
put();
}
}
i’m not sure what you mean but you can also do this :
var x:GameObject;
var ball:GameObject;
var choice:GameObject;
var inserted=false;
function put(obj:GameObject)
{
if(inserted==false)
{
var part=Instantiate(obj,transform.position,transform.rotation);
inserted=true;
}
}
function OnGUI()
{
if (GUI.Button (Rect (10,75,50,50), "X")) {
put(x);
}
if (GUI.Button (Rect (10,10,50,50), "Ball")) {
put(ball);
}
}
But if I want to make the instatiate by mouse?I try this but doesn’t work:
var x:GameObject;
var ball:GameObject;
var choice:GameObject;
var inserted=false;
function OnMouseDown(obj:GameObject)
{
if(inserted==false)
{
var part=Instantiate(obj,transform.position,transform.rotation);
inserted=true;
}
};
function OnGUI()
{
if (GUI.Button (Rect (10,75,50,50), "X")) {
OnMouseDown(x);
}
if (GUI.Button (Rect (10,10,50,50), "Ball")) {
OnMouseDown(ball);
}
}
your combining onMouseDown with Ongui, somehow i think that would give issues. i don’t know which one would get executed first. But again, what error are you getting ?
Also this last script you posted will never work. The onMouseDown is expecting a gameobject input.
Failed to call function OnMouseDown of class Game
Calling function OnMouseDown with no parameters but the function requires 1.
UnityEngine.SendMouseEvents:smile:oSendMouseEvents()
You can’t declare OnMouseDown with a parameter. The error arises because Unity internally tries to call it without any parameters but your version of the function expects a GameObject to be passed.