Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Propellent · Apr 25, 2016 at 04:31 AM · instantiating

Controlling a specific instance of a GameObject clone or otherwise

Hi, I'm making a Space Invaders clone for a school project that must include the use of user-defined data structures. I chose a LinkedList and a Queue made up of a LinkedList. What I would like to be able to do is take the last five enemies in the linked list and set a boolean concerning whether they can fire or not. The reason I want the last five to shoot is because they are, in theory, the only ones with a "clear" shot, meaning they won't collide with enemies below them because there shouldn't be any. My problem is that when I instantiate, I have to cast my scripts into GameObjects(Or at least what I was taught), but since gameobject is the base class I can't get to the variable that says whether they can fire or not.

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
0
Best Answer

Answer by JimNotaro · Apr 25, 2016 at 10:07 AM

What I beleive you are asking is how to access a script on the enemies that has a variable defining if they can fire of not. There are several ways to do this the most effective of them is to use GetComponent to assess the script like this

 YourGameObject.GetComponent<YourScript>.EnableFire = false

obviously you will need to have found the gameObject you were looking for first but this benefits from allowing the same line of code to work on all instances of a gameObject since they will all have the same script name.

You should also not be casting your scripts into gameObjects, they should be components attached to a gameObject aka the enemy gameObject you made.

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 Propellent · Apr 25, 2016 at 01:20 PM 0
Share

Thank you! I'm still getting used to Unity and this helps!

avatar image
0

Answer by fishbrainz · Apr 25, 2016 at 09:23 AM

Get the script you need on the enemy by calling GetComponent() on the enemy gameobjects you stored in your lists

 EnemyScript _enemyScript = List[indexInForLoop].GetComponent<EnemyScript>();
 _enemyScript.yourPublicBoolVariable //your "clear shot" bool variable or w/e

 
Comment
Add comment · 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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Ideal Dynamic Object Generation Technique 1 Answer

Instantiating camera crashes my pc 0 Answers

Not instantiating? 0 Answers

Instantiating in a specific place with force 2 Answers

For loops / lists / instantiating not working! D: 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