Синтаксис:
Используется csharp
using UnityEngine;
using System.Collections;
public class PlayerShoot : MonoBehaviour
{
public Transform puliaPrefab;
void Update ()
{
ShootBase();
}
void ShootBase()
{
if(Input.GetMouseButtonDown(0))
{
Rigidbody clonePylia;
clonePylia = Instantiate(puliaPrefab, GameObject.Find("SootPoint").transform.position, Quaternion.identity);
clonePylia.velocity = transform.TransformDirection(Vector3.forward * 200);
}
}
}
using System.Collections;
public class PlayerShoot : MonoBehaviour
{
public Transform puliaPrefab;
void Update ()
{
ShootBase();
}
void ShootBase()
{
if(Input.GetMouseButtonDown(0))
{
Rigidbody clonePylia;
clonePylia = Instantiate(puliaPrefab, GameObject.Find("SootPoint").transform.position, Quaternion.identity);
clonePylia.velocity = transform.TransformDirection(Vector3.forward * 200);
}
}
}
Выдает ошибку
Assets/Content/Scripts/Client/PlayerControl/Player Control 2D/PlayerShoot.cs(18,25): error CS0266: Cannot implicitly convert type `UnityEngine.Object' to `UnityEngine.Rigidbody'. An explicit conversion exists (are you missing a cast?)
Первым же делом я полез в справку
static function Instantiate (original : Object, position : Vector3, rotation : Quaternion) : Object
Description
Clones the object original and returns the clone.
Clones the object original, places it at position and sets the rotation to rotation, then returns the cloned object. This is essentially the same as using duplicate command (cmd-d) in Unity and then moving the object to the given location. If a game object, component or script instance is passed, Instantiate will clone the entire game object hierarchy, with all children cloned as well. All game objects are activated.
See Also: In depth Prefab Instantiate discussionСинтаксис:Используется csharpusing UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Transform prefab;
void Awake() {
int i = 0;
while (i < 10) {
Instantiate(prefab, new Vector3(i * 2.0F, 0, 0), Quaternion.identity);
i++;
}
}
}
Instantiate is most commonly used to instantiate projectiles, AI Enemies, particle explosions or wrecked object replacements.Синтаксис:Используется csharpusing UnityEngine;
using System.Collections;
public class example : MonoBehaviour {
public Rigidbody projectile;
void Update() {
if (Input.GetButtonDown("Fire1")) {
Rigidbody clone;
clone = Instantiate(projectile, transform.position, transform.rotation);
clone.velocity = transform.TransformDirection(Vector3.forward * 10);
}
}
}
и т.д.
Получается, что один и тот же код у них работает, у меня нет((
Явное приведение типов тоже не помогло.
Синтаксис:
Используется csharp
clonePylia = (Rigidbody)Instantiate(puliaPrefab, GameObject.Find("SootPoint").transform.position, Quaternion.identity);
Клон создается но при этом в логе появляется ошибка
InvalidCastException: Cannot cast from source type to destination type.
PlayerShoot.ShootBase () (at Assets/Content/Scripts/Client/PlayerControl/Player Control 2D/PlayerShoot.cs:18)
PlayerShoot.Update () (at Assets/Content/Scripts/Client/PlayerControl/Player Control 2D/PlayerShoot.cs:10)
Как можно решить эту проблему? Пробовал сделать клон пули и гейм обджект таже фигня. Ему нужен только обджект. А на обджектом я не могу дяльше работать с физикой. Как быть?
Заранее спасибо.