Se the attached file
How do I read the variable there is at turretLevel1, in the script named test?
Like i want something to happen in script “test” if a variable is equal to 10 in the prefab “turretLevel1”
Se the attached file
How do I read the variable there is at turretLevel1, in the script named test?
Like i want something to happen in script “test” if a variable is equal to 10 in the prefab “turretLevel1”
so there is a script on turrentLevel1 in which you want to access a variable?
in this case - and your given hirachy it would work like
//within your test.cs
void Function()
{
if(root.getComponent<yourScriptNameonTurrent1>().yourPublicVariable == 10)
{
//do your stuff
}
}
you can also get the hirachy up by using GetComponentInParent or GetComponentInChildren(), whichever way you prefer
Thanks, i will try it out
Okay, is the part named “root” the name of the prefab, because i am getting this error
The name `root' does not exist in the current context
No, he was pseudo coding. He meant root object. So if you’re working within the class it’d be just GetComponent<> or if it was on some other object it would be someOtherObject.GetComponent<>. Don’t copy paste.
If this isn’t making sense then it sounds like you’ll need to back up a little and learn a bit more about scope and access within both Unity and C#. Unity has great (and super short) video tutorials that explain this really well. Here’s a good one:
thanks for responding jtsmith1287
This is what I was trying to make work:
using UnityEngine;
using System.Collections;
public class test1 : MonoBehaviour {
public bool selected1 = false;
public Transform Tower;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
selected1 = Tower.getComponent<turret>().selected;
// The gameObject wil be the object which is in the root, and contain the script named turret
// The variable is in the script called turret
// The vaiable which is the type boolean is a public named selected
}
}
but it gives me the error
Assets/test1.cs(17,35): error CS1061: Type `UnityEngine.Transform' does not contain a definition for `getComponent' and no extension method `getComponent' of type `UnityEngine.Transform' could be found (are you missing a using directive or an assembly reference?)
I will take a look at your video
Watch out for spelling and capitalization.