Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by PaulCupi · Dec 10, 2012 at 02:05 AM · variablefunctiononmousedownenabled

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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.

Comment
Add comment · Show 11 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image PaulCupi · Dec 10, 2012 at 09:44 AM 0
Share

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?

avatar image clunk47 · Dec 10, 2012 at 04:32 PM 1
Share

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.

avatar image clunk47 · Dec 10, 2012 at 04:34 PM 1
Share

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.

avatar image PaulCupi · Dec 10, 2012 at 05:23 PM 0
Share

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; }

avatar image PaulCupi · Dec 13, 2012 at 09:35 PM 2
Share

You are awesome, works perfect now! Thanks for all your help man!

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges