So I got these lines:
var thrown : GameObject = Network.Instantiate(ball, transform.position, transform.rotation, myParent.networkView.group);
thrown.layer = 10;
thrown.GetComponent("Ball").PassInfoOffline(transform.tag, myParent.networkView.id, myParent.networkView.group, Vector3.forward);
Which are supposed to instantiate an object across the network and set some stuff to the object on the computer of the player who’s instantiating it.
And then I have some code on the object that’s instantiated:
function PassInfoOffline(thisTag : String, thisId : NetworkViewID, thisGroup : int, dir : Vector3){
transform.tag = thisTag;
networkView.RPC("PassInfoOnline", RPCMode.AllBuffered, thisTag, thisId, thisGroup);
rigidbody.velocity = dir*50;
}
However when I instantiate this object, the console says:
Method not found: ‘Ball.PassInfoOffline’.
Why is it not found when I clearly have this function on the instantiated object?
GetComponent(someString) returns an object of type Component, not an object of type Ball. You either need to cast the return or not use the string version (which I would recommend anyway, the string stuff is very sluggish, is slower and the worst thing is that it can just fail due to typos which the compiler will not pick up, if you used it without the string the compiler would tell you the error directly)
Hmm, I did it without the string (thanks for the heads up, didn’t know that the string version is slower) and it said:
Object reference not set to an instance of an object
As if I am referring to a predefined GameObject variable which has not been set.
If it was a public var you can drag the GameObject onto the var in the inspector. This would define the GameObject variable.
But as you can see from the code, the variable’s value is defined during runtime upon object instantiation.
var thrown : GameObject = Network.Instantiate(ball, transform.position, transform.rotation, myParent.networkView.group);
Erm guys, I still cannot figure out why that happens. thrown doesn’t get assigned at all. The object instantiates but the variable doesn’t get the value of the instantiated object. This is a very odd problem. I’ve done this before and I haven’t had any problems. Do you see something that’s not right in the code ?
That error could be about any object you’re accessing on that line of code. Perhaps it’s myParent or myParent.networkView that’s null.
Is the result of Network.Instatiate null, or is the result of thrown.GetComponent null? If the latter, then ‘thrown’ apparently doesn’t have a Ball component attached.
I think that the result of Network.Instantiate is null. The thrown object DOES have a Ball component attached - that was the first thing I checked when I got the error. I tried returning thrown’s tag so I can see if thrown has any value assigned to it and Unity did not return anything. I think I found the problem. Network.Instantiate returns a value of type Object, not GameObject. So the question now is how will I access the game object on the machine that’s instantiating it ?
EDIT:
I did some testing and thrown DOES get assigned. The problem is in this line:
thrown.GetComponent(Ball).PassInfoOffline(transform.tag, myParent.networkView.id, myParent.networkView.group, Vector3.forward);
However I’m sure that the thrown object has the Ball component! It’s a JS script and it’s attached in the prefab.