#hi how do i call a function from a

1 messages · Page 1 of 1 (latest)

cobalt swift
#

hi there

brisk anchor
#

first of all import the script
public <OtherScriptName> <VarName>

the you can reach the variables and methods of said script by using <VarName>.<MethodName>()
copy-pasted it here

cobalt swift
#

i have in script 1 (i want to activate script 1) public static void Roles() {}..... and in script 2 i have (script1).Roles()

brisk anchor
#

also, dont forget to attach the target script in the scene in unity

cobalt swift
#

but that doesnt seem to work

cobalt swift
#

this is a little compicated XD

#

never used that bedore

brisk anchor
#

it took me a while to figure out, but once you have it its quite easy

cobalt swift
#

lemme try it

#

quick question. the public static bool for example. i can use that via other script right? with no problem?

brisk anchor
#

do you need it to be static?

cobalt swift
#

i dont know what static means.... i thaut it was so you can reach it via other script

brisk anchor
#

thats what public does

cobalt swift
#

ah

#

so whats static then? xDDD

brisk anchor
#

afaik you cannot have 2 instances of one static method

#

but honestly, I am not sure

cobalt swift
brisk anchor
#

I am learning myself and from experience, I have neved had to use a static method

cobalt swift
#

mmh... ok

brisk anchor
#

thats why I doubted that you need it

cobalt swift
#

hahahaa yeah

#

ok thanks

brisk anchor
#

how did it go?

cobalt swift
#

got distracted for a bit

#

gimme a min xD

brisk anchor
#

npnp

cobalt swift
#

this is kinda hard to figgure out.. got a blackout.. but ill get there

brisk anchor
#

alright, whats the name of your script 1?

#

I will pretend that its called Script1

#

in the header of script 2 (where you define all the variables that you will be using) you want to put
public Script1 script1

#

and then you can call the method easily anywhere in script 2 by using script1.Roles()

#

last step is finding the script 2 on the object you have attached it to, fining the script1 variable and attaching the Script1 there

#

lemme pull up my own script, one sec

#

hope this helps

cobalt swift
#

yeah i did that

#

that works now

#

its just i am trying somthing new without a tuturial.. and i cant get it to work

#

xD

#

its taking me so long

brisk anchor
#

what are you trying to do? maybe I can help

cobalt swift
#

do you wanna go in a call?

#

or you dont do that?

brisk anchor
#

Im on a train, so that isnt possible, sorry

cobalt swift
#

oh ok

#

emmm...

#

i wanna make roles.. i need 2. so i want role Hunter and Runner... and i need to assighn it to the player and like there can only be 1 hunter

#

but my code is sooooo messy

#

so i got this

#

its multiplayer

#

so that all new for me anyway

#

im testing it now

brisk anchor
#

do you want to choose both of them at the start of the game?

cobalt swift
#

for now i want it so it chooses when i press p

#

i am like on the edge of removing most of my scripts and starting over

brisk anchor
#

what I meant is, do they get their roles assigned at the same time and for the entire game?

cobalt swift
#

same time yes.. and i need it to be resetable

#

so i can do more rounds

brisk anchor
#

alright

#

Im not saying its the best solution out there, but my solution would be putting all the players in a list

cobalt swift
#

more info: you start game, fill in name and press connect,, then when connected to server it loads in a map scene where the player is spawned in

brisk anchor
#

picking a random one and making it the hunter

cobalt swift
#

ohhhhh

brisk anchor
#

then removing the hunter from the list

#

and then looping through the list and making everybody the runner

cobalt swift
#

yeah...

brisk anchor
#

hope that makes sense 😄

cobalt swift
#

i just dont know how to tell my script to make a list and choose 1 xDDD

cobalt swift
#

i can use youtube for that i think

brisk anchor
#

the list is defined by
public List<GameObject> variablename;

cobalt swift
#

imma search it up xD

#

thanks

#

(if you need to explain via text you will have to whrite so much, dont wanna do that to you)

brisk anchor
#

choosing a random thing on the list:
variablename[Random.Range(0,variablename.Count)]

#

this should work

cobalt swift
#

ok

#

imma start over

brisk anchor
#

maybe the C in count needs to be capital

cobalt swift
#

ok hahaha

brisk anchor
#

dont take it as me telling you that you need to start over... I just believe that this is much cleaner and easier to build on

cobalt swift
#

yeah, was planning to anyway, i cant find shit on this code now

#

its spread over like 3 scripts xD

brisk anchor
#

and as for the rese - you can add all the players back into the list again, removing their tags and calling the method Roles() again

cobalt swift
#

yeahh

#

imma take a break first.. and have a fresh start

brisk anchor
#

Aye, programming can be quite mentally exhausting... I'm taking a break myself, I have been working for the past 5 hours lol