0

I have a script which looks like this.

public class Script1: MonoBehaviour
    {
        public Image Character;
    }

I have another script which tries to access the Image like so.

public class Script2: MonoBehaviour
    {
        Script1.Character.sprite = Sprite1;
    }

Of course I get an error because Character is a non-static object. My question is how do I change the sprite in Script1 from Script 2? From what I've read the best way is to define a gameobject in Script2 that references Script1 or use getters/setters. I'm only a couple months into learning C# however and I've never used either of those methods, and every reference I've read doesn't explain it in a way that makes sense to me.

Specifically, if I define a gameobject from Script2, will it create a new gameobject or will it alter the existing one?

public Image Character needs to be accessible to more than one other script eventually, so whatever method I use I need to make sure I'm altering Script 1 rather than creating a new object, or at least it needs to have the same effect.

Could anyone please help with a simple solution?

Max Vollmer
  • 8,412
  • 9
  • 28
  • 43
JCore11
  • 3
  • 2
  • Welcome to Stack Overflow! If you want to use setter/getters, you'll want to make a method in Script1 that returns your character image, and a setter would take the argument of an image and would change the image within Script1. You might want to make a reference of the GameObject in Script2 and use that reference's getter/setters. – Rinktacular Nov 26 '18 at 19:16
  • @Rinktacular, thank you but I'm still a little confused on how to implement that in an efficient way. In my actual script, I have about 20 Images that are being changed in all different ways at different times. I imagine I could use bools and the update function to listen for changes and then call methods that change the image, but that seems really complicated. I've never used getters or setters before. If you could show me a short example of how that would work maybe I could figure it out? Thank you for taking the time to answer my question. – JCore11 Nov 26 '18 at 19:24

1 Answers1

0

You have to create a reference to the script to access its members.

public class Script2 : MonoBehaviour
{
     private Script1 whateverYouWannaCallIt;

     public void Awake()
     {
          whateverYouWannaCallIt = gameObject.GetComponent<Script1>();
     }

}

The line in the Awake function is for if the scripts are on the same object. To get access to a script on another object you need to use Gameobject.Find<>() before the GetComponent<>()

jayj593
  • 68
  • 1
  • 5
  • Thank you, but that didn't seem to work. I have Both scripts as children in the same GameObject. In Script 2 I declared public static Script1 script1, then in the Awake function I did script1 = gameObject.GetComponent(); The result is that while the program runs, whenever the script is references I get NullReferenceException: Object reference not set to an instance of an object. – JCore11 Nov 26 '18 at 20:09
  • I realize the problem is I did gameObject instead of actually including the name of the gameobject. Both scripts are in different game objects, but have the same parents. I'll try to figure out how to use the find method since that must be where I'm getting it wrong. Thank you. If I get it working I'll mark the answer correct. – JCore11 Nov 26 '18 at 20:13
  • I've never used Gameobject.Find before. I'm looking up the documentation and it copying the syntax I'm doing Script1 = GameObject.Find("NameOfParentGameObject") but it says cannot convert type UnityEngine.GameObject to Script1. – JCore11 Nov 26 '18 at 20:17
  • 1
    Got it working, I realized I needed to define the GameObject separately and use Find, and then also define Script 1 and use GetComponent. Thank you so much. – JCore11 Nov 26 '18 at 20:20
  • No problem! It's awesome you got it all worked out by looking at the documentation. That's an important skill to have. Do you mind showing me your new code? – jayj593 Nov 26 '18 at 20:27
  • I would post the whole thing so other people could see but I have about 1,000 lines of code and I had to edit 20+ Images. I created the example code in my question just to simplify what I was trying to do. For defining the objects though I did this. public static GameObject nameOfGameObject; public static Script1 script1; Then in Awake I did this: nameOfGameObject = GameObject.Find("NameOfGameObject"); script1 = nameOfGameObject.GetComponent(); Thank you again, I've been coding a couple months and never quite figured that out until now. – JCore11 Nov 26 '18 at 20:58
  • I added it to my original question since the formatting is weird in the comments. Thank you again. – JCore11 Nov 26 '18 at 21:00