Hello ladies and gentlemen, I am new to unity and I have a quick question. what is the difference between these to codes:
// create a new projectile, use the same position and rotation as the Launcher.
Rigidbody instantiatedProjectile = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0f, 0f, initialSpeed));
// create a new projectile, use the same position and rotation as the Launcher.
var instantiatedProjectile : Rigidbody = Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
instantiatedProjectile.velocity = transform.TransformDirection(Vector3 (0, 0, initialSpeed));
And how do I correct the c# version so I would not get the error CS0266?
Thanks for the response, I know the difference between C# and Java but the meaning of the code is the same and the Error code reads “Cannot implicitly convert type UnityEngine.Object' to UnityEngine.Rigidbody” when I put it in C#.
So why does it work in Java Script and not in c#?
Unity doesn’t use Java, it uses Javascript. If you know the difference between C# and Javascript then you’ll know that C# is a statically (and strongly) typed language and Javascript is not.
Object.Instantiate ( which can be called as just Instantiate ) seems to return an Object, not a Rigidbody. So if you want a rigid body, I think you’ll need to explicitly cast to a rigid body in the code.
I don’t think your JavaScript code is working as you anticipated either. Even though it may compile and run, you should get an InvalidCastException in any language.
I get an error stating that ‘UnityEngine.Object’ does not contain a definition for ‘gameObject’
I even tried using what I assume to be the fully qualified name UnityEngine.Object but it still fails. In looking at the documentation the error seems true as there is no member for gameObject on Object, but why does this work in JavaScript?
edit
Added semi colons. Too much Python coding forgot them when I retyped in browser
Thank you pikusuru, that is what I am saying and yes I do know C#, no need to get smart, I am not here to challenge knowledge. The two codes ask for the same function and get different results, the JavaScript is the first person tut. code and that is what I was converting, so look if you guys want to get flipped mouth don’t answer, I am peaceful and just looking for some help.
You need to define gameObject, use a variable. I’m not entirely sure how to do that in C# but in JScript, you need a variable so you should try out a variable in C#.
// create a new projectile, use the same position and rotation as the Launcher.
Instantiate (projectile, transform.position, transform.rotation);
// Give it an initial forward velocity. The direction is along the z-axis of the missile launcher's transform.
projectile.velocity = transform.TransformDirection(new Vector3 (0f, 0f, initialSpeed));
Now I get the error that this function can not be called and the colliders are not activated don’t know how that happened?
I’m still not clear on what exactly Instantiate returns (I actually haven’t use it so far in my efforts to learn Unity) so I’m not sure if it’s a game object or a component that you instantiate (or can it be both?) Anyway, as I said above Object appears to just be a base class:
So I still think you need to cast. Can you cast it to a GameObject? I don’t have Unity to hand to test any of this at the moment. In retrospect, it might not have been a good idea to try to help without Unity to hand. Speaking of which…
I don’t know what a flipped mouth is, but I know what a chip on a shoulder looks like. If you don’t want my help, I can take a look at some threads from people who aren’t going to accuse me of getting smart or having a flipped mouth when I try to explain something to them. Seriously. It’s no skin off my nose if you don’t want help.
So it seems you have to explicitly cast the Object returned from Instantiate as a GameObject in Boo and C# where as it is implicitly converted to a GameObject in JavaScript.
Seems weird or I’m not getting something but from my tests it works.
And if you want the Rigidbody you then have to get the component from the returned GameObject, remembering to cast the returned Component object to a Rigidbody.
I’m new here as well tmanallen and don’t think sybixsus2 was necessarily being flippant. Admittedly I was thinking the same thing he typed when I initially read the question.
Look dude there are plenty of people on here who could help who are very capable I asked a simple question and to me you gave a smart remark so if you don’t want to help it ain’t no skin off my nose or my back I will figure it out one way or another and yes now I am trying to use a gameobject now. So look you are either in or your out, there are very smart individuals who acutally try to help and not play big shot, I am not here for trouble just help, do it or don’t do it your business man.
I wanted to explain to you that your problem was that you were not casting the result in C#. You asked why it was different in C# than it was in Javascript, and I wanted to explain that the difference was that C# is a strong and statically typed language, whereas Javascript is not. If you had said at the start that you didn’t know much about C#, I would have simply explained it. However, you explicitly told me that you know the difference between C# and Javascript, so I phrased my reply to make it clear I knew that I was reiterating something you almost certainly already knew.
In other words, I took a little extra effort to make it clear that I wasn’t trying to belittle you or make out that you didn’t know something, and that I was only reminding you that this is why it works differently.
Now if you can somehow turn that into me playing big shot, then more power to you. FYI, I’ve only really been using Unity for a few days, and C# for about the same. I’ve plenty of experience in other languages and engines, but I generally make the assumption that everyone on this forum knows more than I do, and that my only ability to help is in narrow areas where my knowledge of other languages/engines may provide some value.
I don’t mind taking a little extra effort to avoid bruising anyone’s ego, but I already gave you that extra effort and apparently it only made things worse, so yes, I think I will bow out. Best of luck with it.
Um… so I won’t say anything about all the other stuff and let that die, but I wanted to correct what I said earlier.
The docs say Instantiate returns an Object but it looks like it returns a GameObject after checking the type? In Boo and JavaScript you can just run with the value but in C# you have to cast it.
Which seems odd to me. I don’t know a lot about C# but I thought if you declare a variable type you’re good if the function returns that type but I had to explicitly convert it.
You can do that with JavaScript, because unseen to you, there is a lot of stuff going on when you do:
var x = 5;
instead of
var x : int = 5;
It’s easier to initially declare the variables without types, yes, but it’s costly on performance if used extensively.
If the function was overloaded to return Rigidbody, GameObject, etc, then you’re right. You wouldn’t have a problem. Instantiate, however, is not overloaded and only returns Object and requires casting. You may not need to cast it in JavaScript, but it is noted in the Unity documentation that it isn’t a good practice to do so.
Thanks Tempest. I get that part of it but what I don’t get is this. Sorry if your explanation is actually clarifying this and I am misunderstanding.
This fails in C# with the can’t implicitly convert source type to destination type error
GameObject myGameObject = Instantiate(somePrefab, transform.position, transform.rotation);
// Even though the following returns UnityEngine.GameObject
// Debug.Log(Instantiate(somePrefab, transform.position, transform.rotation));
But the following equivalent works in Boo, which is what I’m personally using
myGameObject = Instantiate(somePrefab, transform.position, transform.rotation)
# or stating the type explicitly which I think with duck typing is unnecessary
# myGameObject as GameObject = Instantiate(somePrefab, transform.position, transform.rotation)
The stuff in Boo makes sense to me and work as I expect. I don’t get why in C# it seems I have to cast the returned object if it says its a GameObject to begin with. Its probably something about C# I just don’t get.
I think all the code is correct above. Its late and I’m not testing it again right now but those are scenarios I remember from earlier. I’ll check again in the morning.
That appears just to be the limitation of C#. It doesn’t allow for source-destination casting.
I think problem is using Debug in that manner. I suppose there is some conversion there within the Log function, but the actual returned value is of type Object.