Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Change Materialas Instance

Discussion in 'Scripting' started by elliotbra, Jul 18, 2023.

  1. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    Hi,
    I did a looping background and it works perfecctly fine. But when I try to change the material of the background over script, it stopped scrolling. But when I change the material over the inspector it works fine too. I noticed that when I change the material over the inspector, there is an (instance) after the material name, when I change it over script, it isn't there. This is the script that changes the background:
    Code (CSharp):
    1. using System.Collections;
    2.  
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5.  
    6. public class Background : MonoBehaviour
    7. {
    8.     public ShopItemData background;
    9.     public ShopItemData ball;
    10.     private LockerManager lm;
    11.  
    12.     private static Background playerInstance;
    13.     void Awake()
    14.     {
    15.     DontDestroyOnLoad (this.gameObject);
    16.        
    17.     if (playerInstance == null) {
    18.         playerInstance = this;
    19.     } else {
    20.         Destroy(gameObject);
    21.     }
    22.  
    23.     lm = GameObject.Find("GameManager").GetComponent<LockerManager>();
    24.  
    25.     foreach (LockerSlot item in lm.Ballslots)
    26.     {
    27.         if (item.assignedItem.selected == true)
    28.         ball = item.assignedItem;
    29.     }
    30.     foreach (LockerSlot item in lm.BGslots)
    31.     {
    32.         if (item.assignedItem.selected == true)
    33.         background = item.assignedItem;
    34.     }
    35. }
    36.  
    37.     // Update is called once per frame
    38.     void Update()
    39.     {
    40.        
    41.     }
    42. }
    Can someone help me?
     
  2. nontoxicguy1

    nontoxicguy1

    Joined:
    Apr 5, 2023
    Posts:
    8
    My guess here is that you're trying to change the background material with
     background = item.assignedItem; 

    Actually, I think this line changes the value of "background" in your script instance. Unless this is a part of your script (which I doubt since if it is then you would have cut off the part of the script with an empty Update method), this is probably a problem.

    In other words: you're not changing the background material you change what is "background" from your script perspective.

    To fix it, you should probably do something like this:
     background.material = item.assignedItem; 

    It's impossible to tell exactly what you must do because "background" is a ShopItemData which is probably a class you made.

    I'm still confused about things here, like what do you mean by "stopped scrolling"? And what is the exact part of the script that changes material? I still hope I've provided a useful comment that'll help you.
     
  3. elliotbra

    elliotbra

    Joined:
    Jul 21, 2022
    Posts:
    46
    The background variable is just a Storer for what the background shop item data should be right now. Another script actually changes the background material from the background variable (The shop item data Scriptable object has a material variable).
     
  4. ijmmai

    ijmmai

    Joined:
    Jun 9, 2023
    Posts:
    188
    This is confusing. You have an issue happening when the background changes, your first post says "This is the script that changes the background:", but now you mention there is other code that actually changes the background.

    Sounds like you need to share that other code.

    Another thing, take the time to name your variables correctly.
    Having a background of type ShopItemData, and a playerInstance of type Background, can easily result in errors.
     
    nontoxicguy1 likes this.