• 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 sacredgeometry · Aug 14, 2011 at 04:34 PM · gameobjectvariableinheritanceinstancemember

An Inheritance question. ( accessing non-static members from a script component)

Hi i'm struggling with an aspect of unity involving inheritance.

I initially assumed the best way to work would be to derive new classes from GameObjects but as they are a sealed class it has throw a little confusion into the way I would work. I then understood that prefabs were essentially the way around this, you could bundle up your own bespoke GameObjects in the form of prefabs (is this correct) now the problem comes when accessing members of instances of those prefabs. I have set up a bit of code which essentially looks like this.

I have tried it with getComponent on (Cube) and ("Cube") on the instance and it doesn't show that the isActive bool variable is available for setting.

Thank you in advanced

 //----------------------------------------------------------------------------------------
 // First Script
 //----------------------------------------------------------------------------------------
 
 public class GameItem : MonoBehavior
 {
     public bool is Active;
 }
 
 //----------------------------------------------------------------------------------------
 // Second Script ~ Is on a Cube Prefab
 //----------------------------------------------------------------------------------------
 
 public class Cube : GameItem
 {
     isActive = true;
 }
 
 //----------------------------------------------------------------------------------------
 // Third Script
 //----------------------------------------------------------------------------------------
 
 public class CubeEffector : MonoBehavior
 {
     public GameObject cubePrefab; // Holds a reference to the Cube Prefab
 
     GameObject cubeInstance = Instantiate(cubePrefab, transform.position, transform.localRotation);
 
     if(conditions are met){
         cubeInstance.isActive = false;
     }
 
 }
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
1
Best Answer

Answer by DaveA · Aug 14, 2011 at 05:08 PM

try (although there are other syntaxes):

 Cube c = (Cube)cubeInstance.GetComponent(typeof(Cube));
 c.isActive = false;
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 sacredgeometry · Aug 15, 2011 at 09:09 PM 0
Share

wouldnt that just instantiate the cube class without the prefab? i need the prefab because the geometry is part of the prefab ideally cube and the prefab would be the same thing, is there a way to do this? I assumed the best way was to attack a script to the prefab and instantiate the prefab as if you would the class.

thank you for your reply btw

avatar image
0

Answer by AaronG · Aug 14, 2011 at 05:53 PM

You might have to place the 'isActive == true' declaration inside the Awake() function for that class.

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 sacredgeometry · Aug 15, 2011 at 01:18 AM 0
Share

isActive isnt being shown as being available in code completion and throws up an error if i try, its completely out of scope.

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I modify a variable on an instance of a prefab after I created it? 2 Answers

How can I reference in a prefab's script a gameObject found in the scene 1 Answer

Dynamically Changing GameObject makes NullReferenceException(C# Unity) 1 Answer

Problems with the triggers(not recognized) 1 Answer

Spawning random objects. Trouble /w objects. Null Reference. 0 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