Search Unity

Source image None in c#?

Discussion in 'Scripting' started by Unlimited_Energy, Jul 31, 2015.

  1. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    I have code that I need to specify the source image=None

    Image.Sprite = "None"
    its not recognizing None as a string. All of the other Image Files String names work but None is not working in that context, What would you put if there is no source image selected?
     
  2. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
  3. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    if (gameObject.tag == "Slot1" || gameObject.tag == "Slot2" || gameObject.tag == "Slot3" || gameObject.tag == "Slot4" || gameObject.tag == "Slot5" || gameObject.tag == "Slot6" , myImageToUpdate.sprite == null
     
  4. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Code (csharp):
    1. string[] stringArray = new string[] {"Slot1", "Slot2", "Slot3", "Slot4", "Slot5", "Slot6"};
    2.  
    3. if(stringArray.Contains(gameObject.tag))
    4. {
    5.    myImageToUpdate.sprite = null;
    6. }
     
    Unlimited_Energy likes this.
  5. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    I am getting this error.

    Assets/car.cs(12,32): error CS1061: Type `string[]' does not contain a definition for `Contains' and no extension method `Contains' of type `string[]' could be found (are you missing a using directive or an assembly reference?)
     
  6. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    Probably requires System or System.Linq or something, as an extension method
     
    Unlimited_Energy likes this.
  7. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    Why the curly brackets after:

    string[] stringArray =newstring[]

    -------->{"Slot1", "Slot2", "Slot3", "Slot4", "Slot5", "Slot6"}; <--------

    Im reading a book right now on scripting, but it just keeps leading me down other rabbit holes. Im trying to do as I learn because for me, I do not retain if I cannot do the coding at the same time. I read like a quater of the book and I barley retained any of it because I did not script along the way...

    in the book im reading they insert a number into the square brackets like:
    string[] stringArray =newstring[10] and then it creates 10 string names.

    I'm assuming instead of the number ten in the square brackets the curly brackets and string names in parentheses replace the number value in the square brackets?
     
    Last edited: Aug 7, 2015
  8. DonLoquacious

    DonLoquacious

    Joined:
    Feb 24, 2013
    Posts:
    1,667
    When you do:
    Code (csharp):
    1. string[] stringArray = new string[10];
    it initializes a new array with 10 new empty strings- you can think of them like "slots for string variables". If you were to do this with an array of reference types, like custom classes or Objects or GameObjects or w/e, then it would create a 10-slot array of reference addresses. Arrays create/consume all of the space in memory that they need immediately, because they're sequential data storage (that's what makes them so fast to access), so they need to know exactly how much memory is needed when they're initialized (unlike Lists). That's why you can't change the length of an array after it's made, you can only create a new one and overwrite the reference.

    Putting braces after when initializing a collection allows you to give the contents of the collection in-line, so when I say:
    Code (csharp):
    1. string[] stringArray = new string[]{"string", "string", "string", "string"};
    it can see immediately that there's exactly 4 items going into the array, so I don't have to tell it to make 4 slots explicitly. I also won't be able to add a 5th slot later either, because the memory has already been set aside for the array, and it's only for 4 string "slots".

    (Please, no one bring up that strings are technically reference types- it's semantics and confusing and they're not treated the same).
     
    Last edited: Aug 7, 2015
    Unlimited_Energy likes this.
  9. Unlimited_Energy

    Unlimited_Energy

    Joined:
    Jul 10, 2014
    Posts:
    469
    ahh ok, makes sense now. Thanks.