- Home /
Making a var appear on click
what I am trying to achieve is when I click on a block I want a girl to appear in front of me. I have already added the object to the game so I would like it to just appear when this block is clicked and then after about 5 seconds disappear. But before I can address the time I can't get the girl to not be visible when the game starts. This is what I have so far:
var Girl : GameObject;
function Start(){ Girl.enabled = false; }
function OnMouseDown(){ Girl.enabled = true; }
For some reason when I start my game the girl is there before I click the block. I have the variable set right in Unity inspector so where am I going wrong?
Answer by clunk47 · Dec 10, 2012 at 06:04 AM
Go to your project pane. Create a new folder within the assets folder named "Prefabs". Then go to that new prefab folder, right click, then choose "Create > Prefab". Name the prefab. Drag the girl from the scene pane onto the new prefab in the Project pane. You can now delete the girl from the scene. Then you would need to Instantiate the prefab on click event.
var Girl : GameObject;
var spawnPoint : Transform;
function OnMouseDown()
{
var clone;
clone = Instantiate(Girl, spawnPoint.position, Quaternion.identity);
Destroy(clone.gameObject, 3);
}
you can experiment around with the position and rotation of the girl prefab. We need to create the new var clone because we only want to destroy the clone of the prefab, not the actual asset. This should get you on the right path.
Cheers, I will try that. An update is I have tried my old script on a cube rather than the girl and it works fine, my girl is an FBX file does this mean it should be declared as a variable different to GameObject? I don't see why it works with the cube and not with the FBX?
Just create a prefab and drag the fbx model onto it and do as I posted above. $$anonymous$$ake sure you check the scale in FBX Importer options, the older versions of Unity have a default of 0.1, which in Unity 4 it is fixed to 1.0.
So in this instance, you WOULD attach the script to your cube. Because it is an On$$anonymous$$ouseDown() function, and the Girl object will not be in the scene yet, you need the mouse action on the cube. When you click the cube, the girl will be Instantiated from the prefab folder, then destroyed 3 seconds later.
I did it my own way in the end, probably more complicated than it needed to be but I couldn't select the whole FBX file I had to select each child of the part as variables? any way of getting around that? This is what I cam up with and seems to work: var Girl1 : GameObject; var Girl2 : GameObject; var Girl3 : GameObject; var Girl4 : GameObject; var Girl5 : GameObject; var Girl6 : GameObject; var Girl7 : GameObject;
function Start(){ Girl1.active = false; Girl2.active = false; Girl3.active = false; Girl4.active = false; Girl5.active = false; Girl6.active = false; Girl7.active = false; } function On$$anonymous$$ouseDown(){ Girl1.active = true; Girl2.active = true; Girl3.active = true; Girl4.active = true; Girl5.active = true; Girl6.active = true; Girl7.active = true; }
function On$$anonymous$$ouseUp () { yield WaitForSeconds(3.0); Girl1.active = false; Girl2.active = false; Girl3.active = false; Girl4.active = false; Girl5.active = false; Girl6.active = false; Girl7.active = false; }
You are awesome, works perfect now! Thanks for all your help man!
Your answer
![](https://koobas.hobune.stream/wayback/20220613085207im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
onMouseDown call function from other script 1 Answer
two OnMouseDown function in the same obj, it's possible? 4 Answers
iTween - calling functions on oncomplete doesn't work if the function is declared as a variable 1 Answer
How to correctly send a variable to another function? 1 Answer
Error on my javascript code 1 Answer