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
3
Question by FLASHDENMARK · Dec 08, 2010 at 07:39 PM · instantiaterandomsnake

How to Instantiate only once?

Hello i am making a Snake game. i have managed to Instantiate a "apple" once the game is started. But after "picking" the apple up i dont know how to Instantiate ONLY one apple at a random location.

Can you awesome people possibly help me? :)

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

2 Replies

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

Answer by Ehren · Dec 08, 2010 at 10:46 PM

First you need a reference to the apple in a script that is not attached to the apple. Perhaps a script attached to the snake would do the trick.

var apple : GameObject;

You can assign the apple variable using the Inspector.

When the apple is picked up, simply deactivate it in your script:

apple.SetActiveRecursively(false);

Then when you want to spawn it again, re-activate it and set its location:

apple.SetActiveRecursively(true);
apple.transform.position = GetRandomPosition();

If there's no delay between picking it up and respawning, you could just skip the deactivating/activating and set its position to a new value each time it's eaten.

Comment
Add comment · Show 1 · 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 Statement · Dec 08, 2010 at 10:49 PM 0
Share
  • Reuse is good when you can do it :)

avatar image
1
Best Answer

Answer by Statement · Dec 08, 2010 at 09:33 PM

  • First you should have a prefab for your apple.
  • Once this is done, your script must have a link to your prefab.
  • Then it's a simple matter of instantiating the apple at a given time.

See this example code:

// This is your prefab link. // You set it typically in the editor inspector. public GameObject applePrefab;

// Of course you need to call this method from // an appropriate place like OnTriggerEnter or OnCollisionEnter void SpawnApple() { GameObject appleClone = Instantiate(applePrefab) as GameObject; appleClone.position = GetRandomPosition(); // You have to decide what a proper random position is, so // you must write this method. }

See also OnTriggerEnter, OnCollisionEnter.

The reason I tell you about prefabs is that I think it is a common mistake to attempt to use a "live" game object as your prefab. That reference would then become invalid upon destruction and can't be used to instantiate clones properly. Also, beware of trying to clone live game objects either way because they can become hard to manage.

The main problem you seem to have though, is probably that you're checking a reference to see if it exists or not. Once the apple is picked up, the reference becomes invalid (null) and I assume you try to create a new apple. If this is the case, make sure to update that reference directly after instantiating the new prefab or you might run into a case where your script thinks there is no apple out there, while there actually is. I would personally use the events for something like this.

Comment
Add comment · Show 1 · 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 charnew · Feb 09, 2012 at 07:48 AM 0
Share

this is what happened to my game: $$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'UnityEngine.ParticleEmitter.SetActiveRecursively'. Boo.Lang.Runtime.DynamicDispatching.$$anonymous$$ethodDispatcherFactory.ProduceExtensionDispatcher ()

and my script so far: if(theCollision.gameObject.name == "bitter") { transform.eulerAngles = Vector3(1.525879e-05, 180, 219.5672); pill = Instantiate(liquid, fluidflow.transform.position, transform.rotation); pill.SetActiveRecursively(false); Destroy (liquor, 3); }

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

1 Person is following this question.

avatar image

Related Questions

Instantiate a object after destroying a instantiated object. 1 Answer

Randomly Instantiating an object inside a shrinking sphere? 1 Answer

gameobject spawn in randomrange coordinates 1 Answer

instantiate random prefabs based on player camera distance 1 Answer

Random Spawn 2 Answers


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