• 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 DevMerlin · Mar 30, 2014 at 03:45 PM · transformmaterialcolorrenderer

When changing the color of an instantiated object, I keep getting an error - how do I fix this?

I have a couple of pawns I am instantiating and trying to change the color of so that they are more distinct from each other. However, when I try to actually change their color with Renderer.Material.Color = Color.Red, or another equivalent, I keep getting an error from the console:

NullReferenceException: Object reference not set to an instance of an object;

 Transform pawnTwo = gridArray [(gridY - 1), (gridX - 1) / 2] as Transform;
 
 pawnPositionTwo = new Vector3(pawnTwo.position.x, pawnTwo.position.y + 10f, pawnTwo.position.z);
 
 Transform pawnObjectTwo = Instantiate(Pawn, pawnPositionTwo, pawnRotation) as Transform;
 
 pawnObjectTwo.gameObject.renderer.material.color = Color.red;

It's almost like there is a timing error, I'm not sure what else is going on. Note that the code is experimental, it'll be refined later.

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

0 Replies

· Add your reply
  • Sort: 

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

20 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

Related Questions

Changing two different objects renderer colour 1 Answer

Material doesn't have a color property '_Color' 4 Answers

Need material.color to revert. 3 Answers

Changing the color at runtime is causing my material to look very different. 1 Answer

Is it possible to change an entity's RenderMesh color in ECS just like the renderer.material.color in monobehavior? 1 Answer

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