using UnityEngine;
using System.Collections;
public class press_start : MonoBehaviour {
//Sound
public AudioClip startup;
private AudioSource source;
public AudioClip titleScreenMusic;
private AudioSource source2;
//Other
public Sprite regular;
private SpriteRenderer sprite_renderer;
public int press_enter = 0;
//Time
public float timeRemaining = 5;
public int loadHouseLevel = 0;
// Use this for initialization
void Start () {
//press start graphic
GetComponent<SpriteRenderer>().sprite = regular;
sprite_renderer = GetComponent<SpriteRenderer> ();
//sound (startup)
source = GetComponent<AudioSource>();
source2 = GetComponent<AudioSource>();
source2.PlayOneShot(titleScreenMusic, 1F);
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown (KeyCode.Return) && press_enter == 0)
{
press_enter = 1;
sprite_renderer.enabled = false;
source.PlayOneShot(startup, 1F);
}
if (press_enter == 1)
{
timeRemaining -= Time.deltaTime;
}
if (timeRemaining < 0)
{
Application.LoadLevel("path");
loadHouseLevel = 1;
}
}
}
cutscene.cs:
using UnityEngine;
using System.Collections;
public class cutscene : MonoBehaviour {
public Sprite home;
public Sprite inTheMailbox;
private SpriteRenderer sprite_renderer;
public float timeRemaining2 = 4;
// Use this for initialization
void Start () {
GetComponent<SpriteRenderer>().sprite = home;
sprite_renderer = GetComponent<SpriteRenderer>();
GetComponent<press_start>();
}
// Update is called once per frame
void Update () {
if (loadHouseLevel == 1)
{
loadHouseLevel = 2;
}
if (loadHouseLevel == 2)
{
timeRemaining2 -= Time.deltaTime;
}
if (timeRemaining2 > 0)
{
GetComponent<SpriteRenderer>().sprite = inTheMailbox;
}
}
}
Follow BoredMormon’s link. Script-to-script communication is one of the basic skills necessary for Unity, and we’d honestly be doing you a disservice by just providing the line of code you need.
A good way to initiate yourself to a particular programming technic is to isolate it.
Make a new project with only 2 scripts corresponding to the core of your problem, keep it as simple as it gets. Then do trials and errors until you’ve figured it out and are comfortable with it !
using UnityEngine;
using System.Collections;
public class script1 : MonoBehaviour {
public int var1 = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (Input.GetKeyDown(KeyCode.Space))
{
var1 = 1;
}
}
}
script2:
using UnityEngine;
using System.Collections;
public class script2 : MonoBehaviour {
public int var2 = 0;
// Use this for initialization
void Start () {
GetComponent<script1>(var1);
}
// Update is called once per frame
void Update () {
if (var1 == 1)
{
var2 = 1;
}
}
}
It’s good that you started it, now you also need to learn to read compiler errors.
Unity is probably telling you that it doesn’t know the identifiers lines 10 and 15 in your script2. Following the previously shared link should point you in the right direction to fix it !
using UnityEngine;
using System.Collections;
public class script2 : MonoBehaviour {
public int var2 = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (var1 == 1)
{
var2 = 1;
}
script1 otherScript = GetComponent<script1>();
otherScript.var1();
}
}
If it helps, you’re getting closer to the right answer. I’m still not going to hand it to you, but let me see if I can give you some pointers in the right direction:
What you’re ultimately wanting to do is to modify & access var1 on script1 from script2 - var2 does not need to exist, and it’s only confusing the problem. Go ahead and delete that.
Line 20 in the script you posted is exactly right.
When you see someName(), the parentheses on the end mean that it’s a function. I’m guessing you copied that code from the samples on the docs page, which are referencing functions on other objects, whereas you are trying to reference a variable on another object. If var1 were a function, you’d have that line correct! Since var1 is not a function, though, that’s not how you access it. You can set var1 almost like you are (currently) setting var2, you just need to add the otherScript part to that line.
Finally, as @_met44 says, learn to use error messages. If you don’t know how to see them, look at the very bottom of the Unity window - you can click on this part to open the console and see more information, too. (With your script as it is in your last comment, it will definitely be giving you at least one error, on line 16.) If you double-click on the error in the console, it’ll open the script to the line containing the error, and as you get better, you’ll learn what the error messages mean. And even if you don’t know what they mean, definitely include them when asking for help - they help us find your problems!
using UnityEngine;
using System.Collections;
public class script2 : MonoBehaviour {
public int var2 = 0;
ReadMore var1 = GetComponent<script1>();
var1.something = 1;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if (var1 == 1)
{
var2 = 1;
}
}
}
I still can’t get it to work.
I don’t know what “ReadMore” should be replaced with, what “nextTime” should be replaced with, and what “ReadMore” inside ‘<’ these ‘>’ should be replaced with, I don’t know what “something” should be.
And I’m getting an error, line 8, “Unexpected symbol ‘=’ in class, struct or interface member declaration”.