#splitscreen
1 messages · Page 1 of 1 (latest)
Yep
So actually I forgot something
Okay real quickly, do you have a button that brings you from menu to game?
Okay so then are you going to have those checkboxes inside the Menu script?
And does the main menu scene have its own script?
Okay so you know how you can attach a script to a gameObject? Do any of your gameobjects have a main menu script attached to them?
Or do you just have one script for your whole game
Okay, so splitscreen is attached to the splitscreen gameObject?
Okay the easiest way I can think of doing this is making an empty object in your Menu scene and calling it MenuManager or something. I don't work much with the actual Unity interface and I do more scripting so I haven't done it through there before.
Okay and it has a script attached I am assuming?
Okay if you click MenuManager, the only thing it has attached is a script? I
I'll show an example
Except instead of GameManager you would have MenuManager up there
It is a script?
Okay so there are two parts to your splitscreen problem, the first part is making those booleans true and being able to access them, and the second is when they are true making the game splitscreen.
So for the first part
Make an empty gameobject in your menu and just call it MenuManager
Just right click and Create Empty
And just to make it easy did you call it MenuManager without spaces and with those capitals?
Okay so now this is the way I would do it, click on MenuManager on the left and on the right click Add Component and Add Script
New Script
and Call it MenuVariables
or something like that
Give me a min to do something irl brb.
Okay so I just remembered a way to do it, do you have a function for your button that loads the other scene?
when I had that thought and looked it up ppl said it wouldn't work
Back when I was doing something similar
Ok so in that buttons function you should make a playerpref which is just a way of saving settings which is perfect for this situation.
The way you do it is you have an if statement that you can just copy il type it out.
If(vertical)
{
PlayerPrefs.setInt("verticalSplitScreen", 1);
}
If(horizontal)
{
PlayerPrefs.setInt("horizontalSplitScreen", 1);
}
I believe it would be
Let me look into it for a second
My bad
Il fix it one second
I fixed it above
That goes in your button script that changes the scene and it has to be before the line that changes the scene.
You want that in the button that makes it go from Mainmenu to game
Okay so you have a button that when you press it goes into the game
So then do you have a function that changes the scene?
Can I see it?
Oh In unity?
Like the unity app?
Okay, then remember that script that you made
Yeah so in there make a new function below start and update
So public void splitScreen
Or whatever you want to call jt
Then in that function put that script I gave you
With the playerprefs
Yeah remove it from there
Yes
So then in thag button on click in unity add a plus
And drag in your empty objecy
With the global variables attached
Okay
And then that little box add the choose the function that you just made
Whatever you called it
Right
You need to add to the top
One sec
Oh capitalize Set
SetInt
Sure I guess
You forgot the brackets
{}
You need an open bracket at the end of line 23 and 28 after the if
And close brackets on 27 and 33
Yes
Okay now in your button
Yes
Add the empty gameobkrct
Yes
Okay now you need to make your check box work
The vertical and horizontal
Checkbook
Box
So you need to make it so that when you check it it makes it true. When you check horizontal the bool horizontal becomes true.
Ok how did u do it
Can I get a pic
Just to make sure
Lol
Yeah that is waytoodank
Okay perfect and now in the unity inspector did you make it so that oncljck do thosd?
Okay also not fully sure how checkboxes work but will it unxheck the other if they check horizontal
If not u might have to add a line of code to fix that
Okay
Okay now try to apply it and see if it works
To do this
Go to your game scene
And do you have a gamemanager?
Like menumanager?
So then just make an empty object called GameManager
Add a script component and just call it GameManagerScriot
Script
Then inside of it inside of Start add the code for resizing camera
Okay
So the code should be at the beginning something like this
Yes so like this
Horizontal = PlayerPrefs.GetInt("HorizontalSplitScreen");
Then same for vertical
Then below that do an if statement
If (horizontal == 1)
{
Resize camera
}
And same for vertical
Any qs?
Before you go?
Oh also its not HorizontalSplitScreen it has a lowercase h
Also at the top I think you need to declare Horizontal and Vertical to be variables so
Private int Horizontal;
Private int Vertical;
You need to set horizontal = to the playerprefs.getint
same for vertical
Okay and for changing the res
So right now you just have
PlayerPrefs.GetInt
Yes
And also for vertical
Okay now for the camera
Did you add the private int horiz and vertical?
To the top?
Okay now add one more for the camera
I believe it would be private Camera camera;
Yes, it might be Camera instead of GameObject I've never worked with them but yes
Also why do you need 2? So thag one is for the left or top and one is for the right or bottom?
No game object is the type of variable
Okay
For example if I have button I don't do private gameobject buttons1 and 2
I do
Private button buttons1 and 2
Yes
Is it transform?
OkY
So then whay you want to so is remember those two ifs
In the horizontal one you have one camera on top and one on bottom right?
Okay so I am trying to look this part up because I don't play with cameras often
Maybe I'm not fully sure
But you want to make the camera's y divide by 2 for both cameras.
And move camera2 down by the y value of camera2.
Yeah seems like it
Oh the viewport rect?
Yeah I don't touch cameras often as I said
Then you would just play with the values
Okay try it out and see how it goes
See you!
If something is wrong dm me
So I see it.
Gl
Okay
Yeah I got that
Just move it back into the if(horizontal == 1){don't forget these brackets}
Okay now test it
Also real quick add a Debug.Log
after your two playerprefs
that has horizontal + vertical in it
just put (horizontal + vertical)
with the plus
that way it should print 1 and 0 or 0 and 1 in the console
unless none are selected then it would print null or something
no quotes
they are variables
quotes for strings
Looks good I think.
except
the Debug.Log
after the playerpreft
playerprefs
or it will juts give void
What do you mean
stuck on update
Okay one sec
Okay send me the script with the two checkboxes
When you click the box for vertical does the Debug.Log "void vertical is activated" go?
Does it print in console?
What do you mean by star
like turn on the game?
Yeah so both become unchecked?
do you want it to save?
Thats doable
Oh
do you mean play in unity
Maybe your menu has a play button
Okay but thats supposed to be like that
if you want it to save you have to do that, unity by default won't save that
so you have to check it again correct?
it being checked
By default when you click play both will be unchecked
If you want it to save its pretty easy to do that
So if I'm getting this right
you want it to save the selection
even when you unplay and play
In that case, just add something to the start
that says
Okay I see what you mean
Oh i see
Okay
Yes in your start function add something for it to load what you had checked
Right now it just saves
so
you need to put
if(PlayerPrefs.GetInt("horizontalSplitScreen") == 1)
{
//Code for checkbox on horizontal being checked.
}
if(PlayerPrefs.GetInt("verticalSplitScreen") == 1)
{
//Code for checkbox on vertical being checked.
}
I believe in your situation the code for checkbox being checked would just be
horizontal = true;
or
vertical = true;
Does the box check now?
When you go back
Okay it probably has something to do with the horizontal = true part
Just to be completely sure that this is a logic error, I'm going to have you change the way it works a little bit.
So instead of having the function attached to the checkboxes
Can you send me a picture of an example
I can't really understand what you mean by it
Oh I believe I know one of the issues
The PlayerPrefs that you are doing
it saves between games
so you need a spot somewhere where it will just reset it when they reopen the game
Okay so that is a picture of being not in the game
Okay try to check horizontal
then stop the game and start it again
it should have horizontal checked then
And its horizontal right
Yeah I know why its happening I think
Can I get your current code for your MenuVariables script
Lmao
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuVariables : MonoBehaviour
{
public bool horizontal;
public bool vertical;
// Start is called before the first frame update
void Start()
{
PlayerPrefs.SetInt("horizontalSplitScreen", 0);
PlayerPrefs.SetInt("verticalSplitScreen", 0);
}
// Update is called once per frame
void Update()
{
}
public void splitScreen()
{
//Checks if horizontal or vertical is enabled in the first frame of the game
if (horizontal)
{
//Cameras Horiontal orientation
PlayerPrefs.SetInt("horizontalSplitScreen", 1);
Debug.Log("Horizontal Splitscren Set");
}
if (vertical)
{
//Cameras vertical orientation
PlayerPrefs.SetInt("verticalSplitScreen", 1);
Debug.Log("Vertical SplitScreen Set " + vertical);
}
}
public void horizontalTrue()
{
horizontal = true;
vertical = false;
Debug.Log("void Horizontal is activated " + horizontal);
}
public void verticalTrue()
{
vertical = true;
horizontal = false;
Debug.Log("void vertical is activated");
}
}
Copy and paste that into your code
instead of what you have
So just replace what you have now with this
Good now?
Okay
Isn't it supposed to automatically check it off?
So basically you want it to stay checked between games?
In unity does it say vertical is checked
and horizontal isn't?
And if a player goes from the game back to menu vertical is checked
what if none are checked
then what does it do
Okay
Then can I get a screenshot of both checkboxes in unity
The gameobjects
in unity interface
to the right
So i'm talking about the inspector
not the game
except click on the horizontal toggle and take an sc of the right
and vertical
Okay I'm going to fix up the code real quick to make it more dependant on script as opposed to the inspector
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class MenuVariables : MonoBehaviour
{
public bool horizontal;
public bool vertical;
public Toggle horizontal;
public Toggle vertical;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void splitScreen()
{
//Checks if horizontal or vertical is enabled in the first frame of the game
if (horizontal)
{
//Cameras Horiontal orientation
PlayerPrefs.SetInt("horizontalSplitScreen", 1);
Debug.Log("Horizontal Splitscren Set");
}
if (vertical)
{
//Cameras vertical orientation
PlayerPrefs.SetInt("verticalSplitScreen", 1);
Debug.Log("Vertical SplitScreen Set " + vertical);
}
}
public void horizontalTrue()
{
horizontal.isOn = true;
vertical.isOn = false;
Debug.Log("void Horizontal is activated " + horizontal);
}
public void verticalTrue()
{
Debug.Log("void vertical is activated");
vertical.isOn = true;
horizontal.isOn = false;
}
}
Try that maybe?
And make sure to add your toggles to the gameObject
drag them in
Add
using UnityEngine.UI;
to the top
where you have all of the using
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
What line
Oh
never mind
At the top
where it says public toggle
make it horizontalToggle instead of just horizontal
and verticalToggle
I forgot that you are already using horizontal and vertical
public Toggle horizontalToggle;
public Toggle verticalToggle;
Its okay, just change those two lines up top
And see how it work
s
one sec
oh
in your two functions horizontalTrue and verticalTrue
add Toggle to each of the horizontal and verticals
so they become horizontalToggle and verticalToggle
.isOn
go to your two functions at the bottom
yes
Working?
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuVariables : MonoBehaviour
{
public Toggle horizontal;
public Toggle vertical;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void splitScreen()
{
//Checks if horizontal or vertical is enabled in the first frame of the game
if (horizontal.isOn)
{
//Cameras Horiontal orientation
PlayerPrefs.SetInt("horizontalSplitScreen", 1);
Debug.Log("Horizontal Splitscren Set");
}
if (vertical.isOn)
{
//Cameras vertical orientation
PlayerPrefs.SetInt("verticalSplitScreen", 1);
Debug.Log("Vertical SplitScreen Set ");
}
}
public void horizontalTrue()
{
horizontal.isOn = true;
Debug.Log("void Horizontal is activated ");
}
public void verticalTrue()
{
Debug.Log("void vertical is activated");
vertical.isOn = true;
}
}
Copy that
Wait
Okay good
Hmm that means that it is looping but not sure why
Ok try the code again above
I edited it
yes
It might be looping it for some reason
Oh wait I know why
yes
Try that first
if it doesn't work then I'll try my fix
Okay
Ik why
its because you have the function saying if the box is checked then check the box
I'm just dumb
Remember you have that onclick
so if it is clicked then it will check it
One sec let me think about it
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuVariables : MonoBehaviour
{
public Toggle horizontal;
public Toggle vertical;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void splitScreen()
{
//Checks if horizontal or vertical is enabled in the first frame of the game
if (horizontal.isOn)
{
//Cameras Horiontal orientation
PlayerPrefs.SetInt("horizontalSplitScreen", 1);
Debug.Log("Horizontal Splitscren Set");
}
if (vertical.isOn)
{
//Cameras vertical orientation
PlayerPrefs.SetInt("verticalSplitScreen", 1);
Debug.Log("Vertical SplitScreen Set ");
}
}
public void horizontalTrue()
{
vertical.isOn = false;
Debug.Log("void Horizontal is activated ");
}
public void verticalTrue()
{
Debug.Log("void vertical is activated");
horizontal.isOn = false;
}
}
Try that
And if it doesn't work I have a rewritten version that should
yes
after u copy that code
Okay then this should work
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuVariables : MonoBehaviour
{
public Toggle horizontal;
public Toggle vertical;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(horizontal.isOn)
{
vertical.isOn = false;
}
else
{
horizontal.isOn = false;
}
}
public void splitScreen()
{
//Checks if horizontal or vertical is enabled in the first frame of the game
if (horizontal.isOn)
{
//Cameras Horiontal orientation
PlayerPrefs.SetInt("horizontalSplitScreen", 1);
Debug.Log("Horizontal Splitscren Set");
}
if (vertical.isOn)
{
//Cameras vertical orientation
PlayerPrefs.SetInt("verticalSplitScreen", 1);
Debug.Log("Vertical SplitScreen Set ");
}
}
}
This gets rid of the functions
so make sure you get rid of them in the editor
and also go to edit in unity
and at the bottom
clearAllPlayerPrefs
Just go to each of the toggles
in your unity
and on the right
click minus
Ok one more time just so I am getting this right, what happens is that vertical is checked
and when you check horizontal it checks both?
did you do edit> clear playerprefs?
Let me see the other code
that has the splitscreen
probably
quick question is it a 2d or 3d
I'm going to try to recreate your project
Not sure why but unity is giving me hella errors when I load it
You could try that
you need the = 1 to be out side the paranthesis
and its ==
because you are comparing
= is setting the value to
also you need a parenthesis around the whole Player prefs
the == is outside too
PlayerPrefs.GetInt is a function
So it runs the function and sees if it gets 1
if (PlayerPrefs.GetInt("horizontalSplitScreen") == 1)
The parenthesis is for the if
If it was without the parenthesis if would treat it like
if PlayerPrefs.GetInt("horizontalSplitScreen")
then == 1
and it would be like why is there just a random == 1
if ( ) is true then run this
so if (Playerprefs value is 1) then run this
so if (PlayerPrefs.GetInt("horizontalSplitScreen") == 1)
yes
Yeah I'm testing it
Just wonderwing
When does splitscreen run
When you click play?
the function
Okay
Okay I'm asking why you have a splitscreen function
does it actually run
Okay
got it
I'm trying to rewrite it to make it simpler
Okay then
Oh shit
Dude
I literally remember saying how to fix this
before any of this was started
I was saying like in the start function before all of these lines put in PlayerPrefs.SetInt("blah", 0) for both
then I forgor 💀
Does it still work if you have both unchecked though?
Like if you have both unchecked will it make a normal camera
Its okay not too hard
Just hide the camera instead of deleting
and do the rect transform just with (0, 0, 1, 1)
So it would be cam1.rect = new Rect(0f, 0f, 1f, 1f);
cam2.active = false;
So where it says if (vertical == 1) and if (horizontal == 1) in your Gamemanager below it just add this and you literally are done
if (horizontal == 0 && vertical == 0)
{
cam1.rect = new Rect(0f, 0f, 1f, 1f);
cam2.active = false;
}
Yeah np
I think they get auto deleted
but if you just paste that above you can be done
it goes after the horiz and vert if statements in gameManager on line
38 ish
Okay
Okay
Why didn't it work
You can still do it like that
Here, just do it something like this
In your MenuManager make a function
public void Singleplayer()
and inside of it put the line
PlayerPrefs.SetInt("singleplayer", 1);
and in your MenuManager start function also put PlayerPrefs.SetInt("singleplayer", 0);
So in MenuManager
Start:
PlayerPrefs.SetInt("singleplayer", 0);
New Function named Singleplayer:
PlayerPrefs.SetInt("singleplayer", 1);
Then finally, in GameManager on your other scene
At the bottom of Start:
if (PlayerPrefs.GetInt("singleplayer") == 1)
{
cam1.rect = new Rect(0f, 0f, 1f, 1f);
cam2.active = false;
}
And in the other functions you need to add that it cannot be singleplayer for splitscreen.
So in the end it would look something like
void Start()
{
horizontal = PlayerPrefs.GetInt("horizontalSplitScreen");
vertical = PlayerPrefs.GetInt("verticalSplitScreen");
singleplayer = PlayerPrefs.GetInt("singleplayer");
Debug.Log(horizontal + vertical);
if (vertical == 1 && singleplayer == 0)
{
//Resize vertical
Debug.Log("Vertical");
cam1.rect = new Rect(0.5f, 0f, 0.5f, 1f);
cam2.rect = new Rect(0f, 0f, 0.5f, 1f);
// x y w h
}
if (horizontal == 1 && singleplayer == 0)
{
//Resize horizontal
Debug.Log("Horizontal");
cam1.rect = new Rect(0f, 0.5f, 1f, 0.5f);
cam2.rect = new Rect(0f, 0f, 1f, 0.5f);
// x y w h
}
if (singleplayer == 1)
{
cam1.rect = new Rect(0f, 0f, 1f, 1f);
cam2.active = false;
}
}
Note I also added a singleplayer variable.
and I also added singleplayer to the other if statements.
I forgot
at the top make singleplayer an int
so above start
yes
should work
enabled
instead of active
it should work
Okay yeah
that should fix it
lol
it means you understood
so thats good
Okay
cursors?
Oh I see
Let me see the code that manages the controls
like the cursors
it is gameObject.enabled = false;
So can I see the cursors scrip
t
Or did you use Unity Movement system
Yes
So you used the movement system?
Yeah sure
Do the cursors actually move?
crosshair
okay then yeah just turn off the image
It should be a canvas
not just a gameobject
its in UI > canvas
Then in your GameManager
add a line to the singleplayer if in start
and a variable to the top
public Canvas cursor2Canvas;
So first add the variable
to the top
public Canvad cursor2Canvas;
then in the if statement that is if (singleplayer == 1)
just add canvas.enabled = false;
and drag in the cursor2's canvas
yes
Yep
Okay
are you talking about the Player2
or the cursor
Okay and what is player2
is it an image
or
Oh like if you look there from player1
you can see player2
its a human
ok
send me a picture of the inspector
the one on the right
when you click player2
the gameobject
Okay
it would be SetActive = aflse
false
instead of enabled
🍦 bing 🥶 chilling
Oh
what is the line of code
ooh
it isn't SetActive = false
it is gameobject.SetActive(false);
its a function
not a property
Yeah a canvas
is what you put the cursors into
Wait what are you trying to do again
just make it simple and make a variable
public gameobject cursor2;
drag in the cursos
and SetActive(false);
Oh
Ok then click the arrow to the left of canvas 1 and canvas 2
and take a screenshot of the whole thing
so i can understand
Okay
I get it
so real quickly
a canvas is like an artists canvas
its where you make stuff for UI
so you have two canvases, one for player 1 and one for player2
Yea
okay
so are these canvases in the same place as GameManage
r
same scene
Okay so in GameManager
add two variables
public Image cursor1;
okay
public Image cursor2;
then in the vertical if statement
wait actually
you want them to be canvases
not images
so public Canvas cursor2
yes
;
I think so
it could also be SetActive(false);
try both
you have two canvases
you have a game object
and in the game object you have two canvases
and in each canvas you have a cursor
image
so drag the canvases
ah yes
Oh okay
in that case Cursorpart are your canvases
so drag those in
Oh what
Why
Oh okay
Then in that case make the variables the gameObject
ok
public GameObject cursorPlayer1;
public GameObject cursorPlayer2;
Okay
So then in vertical if you want to adjust the cursors x y width height
Yeah
you probably want
cursorPlayer2.position = new Vector3(x, y, z);
You can use rect
yeah sure
Not sure what you mean.
You need to move the cursors yes
then just use rect on the game object
yeah but the gameobject contains the images
oh then just use vector
So what if it moves all images
isn't that the point
The gameobject contains all of the images needed to make 1 plus correct?
and you have two gameobjects?
so then what is the problem?
That doesn't matter
It'l keep that
Thats on the image yes
not the gameobject
So cursorPlayer2.rect = new Rect();
first try putting random digits to make sure that you don't get an error
between 0 and 1
first do vertical
So try cursorPlayer2.rect = new Rect(0, 1, 1, 1);
or something
Just make sure you can do rect on a gameobject.
yes
yes
one sec
Oh yeah
Okay
then you have to use Vector3
so it would be
cursorPlayer2.position = new Vector3(x, y, z);
i don't think you need the floats
it should take integers
btw the f means float
so any number with decimal
my bad
yeah
Yes
Okay and did you change the xyz
yeah
why would it change
if you didn't give it a value to change to
just mess around with it until it works
oh
try 2 2 2
see what it does
Are you sure that you are actually making the if statement true
like are you going into vertical mode if it is in the vertical if
wait
check the transformations
what is the xyz exactly now
just like you did above
Yeah
Okay just make sure that the values for the gameobject aren't changing.
Also add a Debug.Log(cursorPlayer2.transform.position);
Oh
Ok
then you probably want a foreach loop
try this
foreach (Canvas c in cursorPlayer2)
{
c.transform.position = new Vector3(2, 2, 2);
}
you can try that
And if that doesn't work
foreach (Canvas c in cursorPlayer2)
{
foreach (Image i in c)
{
i.transform.position = new Vector3(2, 2, 2);
}
}
Try this first
then yeah you probably want to do your way
lol
you put this instead of the cursorPlayer2.transform.position
Eh just try it lol
just for fun
yea
lool
Ok just do your idea
that was just a dumb way lol
oh what you need is an IEumerator class
that is derived from MonoBehavior
but also derived from your function
and then compiled into a class
that returns an IEun
you getting this
?
I'm kidding
just do your mspaint thing
or if you want send it over il do it in photoshop
okay
ok
send the parts
what resolution u want
yea
or do you want a new crosshair
Okay so a dot
eeh
Want it like this or more a plus
transparent version
Ok
Two optios
oh
ok
Alr its 1.21am here
I'ma go sleep
Need anything else b4 i go?
yea
probably
not fully sure what the code is
can i see the code
Just use Vector3
so transform.position
or whatever it was
= new Vector3(x,y,z);
you put -450
rect has a max of 1
oh
cam1.rect = new Rect(0f, 0.5f, 1f, 0.5f);
but why it just rect there
Ok whatever
Just try it
nah the recttransform
but u can try vector 2
idk
yeah thats why I think vector easier
btw z stays the same I think
why can't u use image
instead of rawimage
just make it a sprite
probably
ok
Yeah but u want it like this
imageVariable.GetComponent<RectTransform>().anchoredPosition = new Vector3(xPos, yPos, zPos);
nah you dont keep the imageVariable
cursorPlayer2 is the imageVariable
Ok
works?
ok
yeah
What is it @fast atlas