• 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 Hoffa25 · Nov 25, 2014 at 08:33 AM · imagechange

How to change an images source image from script? C#

I have been stuck on this for several hours now and I cant find any answer that I understand here or on google:/. Being a beginner at Unity and C# please try to explain for an idiot:)

I have my Image "testImage" decleared in my script and I have dragged the Image gameobject to the slot in inspector. In my project assets I have the other source image that I want to change to called "NewSourceImage".

   public Image testImage;
     
   public void Change()
     
     {
       testImage = GetComponent<Image>("NewSourceImage");
     }

I get the error message: error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)' cannot be used with the type arguments

Comment
Add comment · Show 2
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 Hoffa25 · Nov 25, 2014 at 10:29 AM 0
Share

Ok, thanks but I tried as you wrote and get this error message now:

error CS0308: The non-generic method `UnityEngine.Component.GetComponent(System.Type)' cannot be used with the type arguments

avatar image CHPedersen · Nov 25, 2014 at 10:35 AM 0
Share

You're passing type parameters to a method that is not generic. Update your question with the changes you made to your code, or we have no chance to fix it. This isn't Delphi, and I'm not the oracle of it. :)

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by CHPedersen · Nov 25, 2014 at 08:34 AM

In C#, there should be no period before the type parameter brackets:

 testImage = GetComponent<Image>("NewSourceImage");
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.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The variables and functions 2 Answers

is this correct way to change var from other script? 1 Answer

Is it possible to change an images source image by code? C# 2 Answers

Changing GameObject texture? 4 Answers

How to change an image after it turned 90 degree 1 Answer

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