• 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 /
  • Help Room /
avatar image
0
Question by DanLevi · May 21, 2016 at 03:04 PM · c#instantiatecomponenttypecasting

Why isn't this instantiation cast working?

So i am trying to directly cast a instantiated object as my custom component ClickableTile.

 TileType tt = tileTypes[tiles[x, y]];
 var tile = Instantiate(tt.tileVisualPrefab, new Vector3(x, y, 0), Quaternion.identity) as ClickableTile;
 Debug.Log(tile);

This returns null.

ClickableTile.cs looks like this and are attached to the prefab:

 public class ClickableTile : MonoBehaviour
 {
     public int tileX;
     public int tileY;
     public TileMap map;
   
     void OnMouseUp()
     {
         Debug.Log("Click!");
 
         map.MoveSelectedUnitTo(tileX, tileY);
     }
 }

Why isnt this working? I thought that we could cast to any component on object?

This works:

 TileType tt = tileTypes[tiles[x, y]];
 GameObject tile = (GameObject)Instantiate(tt.tileVisualPrefab, new Vector3(x, y, 0), Quaternion.identity);
 ClickableTile ct = tile.GetComponent<ClickableTile>();

Something i am missing?

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

Answer by jgodfrey · May 21, 2016 at 03:24 PM

I assume your "ClickableTile" script has just been added (as a component) to a standard GameObject that you've turned into a prefab, right? if that's the case, the object you're instantiating isn't a "ClickableTile", it's a GameObject that has a ClickableTile component attached to it.

To get access to the ClickableTile script, you need to instantiate the prefab as a GameObject, and then get a reference to the ClickableTile component. So, something like this:

 GameObject tile = (GameObject)Instantiate(tt.tileVisualPrefab, new Vector3(x, y, 0), Quaternion.identity);
 var ct = tile.GetComponent<ClickableTile>();
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 DanLevi · May 21, 2016 at 03:45 PM 0
Share

Yes that is the case. I was assuming i could just cast it to a ClickableTile, i am sure i saw something like this in a tutorial i watched earlier. Well, thanks, i guess that is cleared up.

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

158 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Start coroutines when the class is not a MonoBehaviour 1 Answer

My components are disappears 1 Answer

Game Manager can't decide what the instance of the object is despite just making an instance of it 1 Answer

Instantiate particle system attached to object 1 Answer

Cannot destroy the SphereCollider on Instantiated Objects 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges