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 Wyko · Jun 24, 2011 at 08:34 PM · function

Using Variables from Parent Class (And Instantiate)

Hey everyone!

I have an array of custom objects representing tiles on a board. Inside each object is a function to instantiate a prefab. For some reason, this function works well when it's in a script in the Start() function of my game controller, but not when it's buried in the class.

When compiled, Unity tells me that it can't find tilePrefab, TileArray, and it has no clue what Instantiate is.

I know that this is an obvious problem but I can't quite figure it out.

The stipped-down version of the code in question:

 // In GameController.js
 
 public var tilePrefab : Object; 
 public var TileArray : Tile[,];
 
 function InitialGridLoadup (){
     tilePrefab = Resources.LoadAssetAtPath("Assets/Prefabs/Tile/Tile.prefab", Object);
         
     for ( var x : int = 0; x < mapX; x++ ) {
         for ( var z : int = 0; z < mapZ; z++ ) {
             // Creates the tile
             TileArray[x, z] = new Tile(type, false, 10, x, z);
             TileArray[x, z].CreateTile();        
         }
     }
 }
 
 class Tile{ 
     var type : int;
     var visible : boolean = false;
     var health : int;
     var x : float;
     var z : float;
     
     function CreateTile () {
         switch (type){
         case 0 :
             var tile : Transform = Instantiate(tilePrefab.transform, new Vector3(x,0,z),Quaternion.identity);
             tile.name = TileArray[x, z].Name();
             break;
         }
 }




As a super added bonus, does anyone know a streamlined way to replace a GameObject with another prefab at runtime? I need a function here to replace one type of tile with another.

Comment
Add comment · Show 3
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 Dreamblur · Jun 24, 2011 at 09:18 PM 0
Share

I tried your script and it seems to me that static functions have to be called statically when inherited by a UnityScript class (i.e., Object.Instantiate rather than simply Instantiate). As for the variables not being recognized, it's because a script written in UnityScript is implicitly treated as its own class (like the explicit class declaration in C#). Obviously, that would make them outside the scope of another class, even if that class is found in the same script.

avatar image Wyko · Jun 24, 2011 at 09:25 PM 0
Share

Good call. I need the variables to be declared in a parent though, because I get the feeling that loading the prefab asset for every instance of the tile class is going to hit it really hard.

avatar image Dreamblur · Jun 24, 2011 at 10:02 PM 0
Share

It seems to me that you're not instantiating this particular script and that you don't really need tilePrefab and TileArray to be serialized, so you can just make those variables static.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by testure · Jun 24, 2011 at 08:48 PM

Instantiate is a member of MonoBehaviour, you can't just call it from a class that's not derived from it. If you're using a singleton game manager, you cold make a static function called Instantiate and call it using GameManager.Instantiate

other than that, you're going to have to find another way to do what you're trying to do.

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 Dreamblur · Jun 24, 2011 at 09:00 PM 1
Share

Instantiate is only an inherited member of $$anonymous$$onoBehaviour. It is inherited from System.Object, which all classes derive from, so every class has Instantiate.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

[SOLVED] Cycling all the way through weapons properly 1 Answer

How do you check what function your animation is calling? 0 Answers

How to call function from another script on an instantiated Prefab? 2 Answers

Help with Functions 3 Answers

Array Element number 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