I apologize up front for this. I know there are other threads for this but I just can’t get it.
I just migrated my game from Unity 2.5 Windows to iPhone basic. I expected to get some Typing errors but…wow! 150+! I have managed to kill a lot of them, and I’m down to two basic errors.
I’ve read about these issues but I don’t think I will understand them without some help with an example from my own code. Both errors deal with passing variables between two objects.
Receiving the error “‘levelsUnlocked’ is not a member of ‘UnityEngine.Component’” with this code.
private var objGlobal : ctr_global;
var numLevels : int;
function Start (){
//SET GLOBAL OBJECT
objGlobal = gameObject.FindObjectOfType(ctr_global);
numLevels = objGlobal.GetComponent(ctr_global).levelsUnlocked;
}
Next is the error " ‘GetComponent’ is not a member of ‘Object’."
private var objAudioSrc;
private var audioScript;
function Start(){
objAudioSrc = GameObject.Find("audiosrc1");
audioScript = objAudioSrc.GetComponent(pinaudio);
}
As I said, I know this has been covered here but I’m just not getting it while looking at other peoples’ code. I just set up Unity iPhone yesterday and was overwhelmed at the errors I got. :shock: I’m finally left with solving these two types.
If there is missing code that you need to see just let me know. I’m an “advanced novice” coder in general and some of the terminology used to help others is a bit over my head.
My sincere thanks to anyone who is willing to help straighten me out here.
On the first part, you are getting objects of type ctr_global as you are using as a component type, right? If so, which is it, a object type or a component type?
// Getting object type
objGlobal = gameObject.FindObjectOfType(ctr_global);
// Getting component type (should be different than object type)
numLevels = objGlobal.GetComponent(ctr_global).levelsUnlocked;
Next, you need to set the type of the vars. For example, you should use the following for the object:
private var objAudioSrc : GameObject;
I think that will help you. I’m a newb myself to Unity though so take it with a grain of salt.
Okay well the errors disappeared. Of course there are so many more errors I have to fix before I’ll know if it will work haha.
This bit of code was in function Start() so I guess I’ll have to move it to Update. i actually moved the first line up to where my other variables are.
I’ll give this a try in regular Unity and see how that goes and report back. Thanks man!
objGlobal = gameObject.FindObjectOfType(ctr_global);
var someScript : ctr_global = objGlobal.GetComponent(ctr_global);
var numLevels : int = someScript.levelsUnlocked;
The error went away. I couldn’t test in unity iPhone, so I went back to Unity 2.5 on my PC and made the same code change. The game works. So I assume it will also work in the iPhone version.
I did need to move the last 2 lines out of function Start since since the variables are declared there, but that should be okay. Will this slow down the game at all?
If I can incorporate this into my other script errors I will be so happy.
I am going to buy, then review, your game War3100 as a small token of my thanks. I really appreciate all of this!!!
If you can, keep it in Start() as that will only be called once. If you put it in Update() the code (and CPU) will have to find the object every game update cycle.
You could try:
// Up in globals
var someScript : ctr_global;
var numLevels : int;
function Start()
{
objGlobal = gameObject.FindObjectOfType(ctr_global);
someScript = objGlobal.GetComponent(ctr_global);
}
function Update()
{
numLevels = someScript.levelsUnlocked;
}
I’m gonna take a deep breath now and enjoy the small success here. Afterward, I will knock out the rest of these errors and hopefully try and play this thing!
And as I said, helping you out a small bit with a purchase and review is a small token of appreciation for the help you gave me and time spent. Thanks again!
One more little problem I’ve encountered while squashing these bugs.
Rather than trying to read a variable, I am trying to write to another objects variable here. I can’t get the “not a member of ‘UnityEngine.Component’” error to disappear.
In this one, I am trying to write to the variable integer “resetNow” in the script named “PinReset”, in object “pin1”.
var object1 : PinReset;
var script1b : PinReset;
function Update(){
object1 = gameObject.FindObjectOfType(PinReset);
script1b = object1.GetComponent(PinReset);
script1b.resetNow=1;
}
You’re doing the same thing again. There is a fundamental difference between a game object, meaning an instance of the GameObject type, and a component. You need to pause and review the documentation to understand the difference between the two. While you work through all the issues of doing the port, you should take advantage of these errors to understand what they mean, rather than just focusing on getting past them. This will be a great learning experience.
At any rate, GetComponent is a method of GameObject, not component.
This line of code:
someScript = objGlobal.GetComponent(ctr_global);
Is calling a method on a component of type ctr_global that doesn’t exist. Hence the error message:
But that code isn’t throwing out an error. I tried as you suggested…here it is:
var object1 : PinReset;
var script1b : PinReset;
function Update(){
script1b = gameObject.GetComponent(PinReset);
script1b.resetNow=1;
}
Perhaps I’m declaring the variables wrong now.
I’m gonna move the appropriate code to Start as well once it works.
EDIT:
I understand, pretty much, getting a component from an object, and what the difference is. I just can’t figure out what Unity iPhone wants from me. I’m doing as you suggested and reading through the documentation. Perhaps I should use the Inspector and make things simpler.
When I imported my project into Unity iPhone, it rearranged my directory structure. I was editing a duplicate script…none of my earlier changes were taking affect in the console!
It works now. This is my first Mac and…AAAARRRGH!!!
But I’m happy it works now. My very first code change would have worked if I’d known.
I’m using the inspector to solve some of this stuff. I added:
var object1 : GameObject;
var object2 : GameObject;
var object3 : GameObject;
var object4 : GameObject;
var object5 : GameObject;
var object6 : GameObject;
var object7 : GameObject;
var object8 : GameObject;
var object9 : GameObject;
var object10 : GameObject;
But they do not show in the inspector. Is this because there are still errors in my other scripts? I’m still working on them.
I imported this fixed code into Unity 2.5 (pc) and it works fine. So is this just a Unity iPhone thing? I’d just like to set up all my objects now rather than later.
As long as you have compiler errors, new things that you add to the script won’t show in the inspector. You either need to fix the compiler errors first, or comment out the code that is causing the errors.