#how do you get a varible from another script to a new script

1 messages ยท Page 1 of 1 (latest)

tired hare
#

what engine are you using

limber cairn
tired hare
limber cairn
#

yeah i watched that video already it didnt really work

robust lichen
#

show code

#

general steps:
1.) your script has to get the component/script attached to another object
2.) once you have the component, you have to get it with .VariableName

limber cairn
#

ok give me a sec

robust lichen
#

make sure that .VariableName is public

limber cairn
#

okay it is

#

i want this varible

#

variable

#

in this script

limber cairn
robust lichen
#

use GetComponent<ClassName>() to get it

#

so GetComponent<Portalpont>()

limber cairn
#

oky i will try

limber cairn
robust lichen
#

GetComponent<Portalpont>() will get the script, so now you have to add .pointadd to it

#
Portalpont theComponent = GetComponent<Portalpont>();
int theVariable = theComponent.pointadd;
limber cairn
#

and i put all of that in void start??

robust lichen
#

are you trying to keep the values in both scripts synced?

#

if you put it in the start it will copy the value once

limber cairn
#

oh okay

#

dang

#

i still dont have the variable

robust lichen
#

theComponent.pointadd instead

limber cairn
#

error

robust lichen
#

you need to get the component

#

GetComponent<Portalpont>().pointadd

limber cairn
#

that does not seem to work

robust lichen
#

what does the error say?

#

you also don't need the last one, just do theComponent.pointadd

limber cairn
#

Only assignment, call, increment, decrement, and new object expressions can be used as a statement

robust lichen
robust lichen
# limber cairn what last one

you already got the component through theComponent. it's stored there. you can just do theComponent.pointadd += point